SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Trying to get property of non-object?

    Trying to get property of non-object? in reference to // $thac0 = $data->records->row->strength;
    the following function is in my Code-Igniter framework, but it keeps giving me an error message that $thac0 is trying to get the property of a non-object...
    Can anyone spot my mistake?


    Code:
    function thac(){
    
    $agrsr = trim($this->input->post('agrsr')) ;
    $total = trim($this->input->post('total')) ;
    $weapon = trim($this->input->post('weapon')) ;
    $target = trim($this->input->post('target')) ;
    
    $this->load->model('membership_model');
    $data['records'] = $this->membership_model->getDfnsvTrgt($target);
    
    $thac0 = $data->records->row->strength;
    
    $array = array('total'=>$total, 'weapon'=>$weapon, 'target'=>$target, 'thac0'=>$thac0 );
    
    echo json_encode($array);
    
    
    }
    I'm also including it's call out to getDfnsvTrgt($target);

    Code:
    function getDfnsvTrgt($target)
    	{
    		$this->db->where('username', $target);
    		$query = $this->db->get('users');
    		
    		if($query->num_rows == 1)
    		{
    			foreach ($query->result() as $row) {
    			    $data[] = $row;
    			}
    		return $data;
    		}
    		
    	}

  2. #2
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $data['records'] = $this->membership_model->getDfnsvTrgt($target);

    $thac0 = $data->records->row->strength;
    You assign the result to an array. Then you try to use the array as an object. Next, the returned query result is an array as well which you try to use as a object.Truth be told I find the getDfnsvTrgt function confusing. I think something like this is what you want.
    Code PHP:
    $thac0 = $data['records'][0]->strength;
    Basically you should select a value (a row in this case)of the query result and that value probably is an object on which you can call the property.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thank you... now I understand. I'm still pretty new to the field. Thank you so much!

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question exchange the explicit field name for a variable?

    Could I exchange the explicit field name for a variable representing one of five fields I expect to be in the data base?
    Code:
    $thac0 = $data['records'][0]->$field_I_hope_to_find;

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •