CodeIgniter Forums

Full Version: having issues with Error: Trying to get Property of Non Object. Help!!!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]reghan[/eluser]
Hello I am getting the error trying to get property of non object.
here is my code

Model:
Code:
<?php
class Department_model extends CI_Model {

public function __construct()
{
  $this->load->database();
}

function depart_table_list() //list the departments names
{
  $this->db->order_by('DepartmentName', 'asc');
  return $this->db->get('department');
}


// get department by id
function get_by_id($id){

  $this->db->where('DepartmentId', $id);
  return $this->db->get('Department');
}

}
Controller function:
Code:
function get_dept_info()
{

  $id = $this->uri->segment(3);
  $dept_info = $this->department_model->get_by_id($id);
  $data['department'] = $dept_info->result();


                $this->load->view('dept_info', $data);

}
view:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
<h3 align="center">Business Impact Analysis(BIA) Questionnaire</h3>
<h5 align="center">&lt;?php echo $department->DepartmentName; ?&gt;</h5>








&lt;/body&gt;
&lt;/html&gt;

I basically am just looking to print out the department name based on the id selected. I feel like this should be simple and am not really sure where I am going wrong.

Thanks to all who reply!!

El Forum

[eluser]mudshark[/eluser]
Are you loading the Department_model in your controller's constructor?
Also, your table is either "Department" or "department" - you have both in your model.

El Forum

[eluser]weboap[/eluser]
i think your issue is you are using ->result() instead of ->row()

another way to do things.

Code:
//in model

// get department by id
function get_by_id($id){
      $id=(int)$id;
      $this->db->where('DepartmentId', $id);
      $this->db->limit( 1 );
      $query = $this->db->get('Department');

         if( $query->num_rows() > 0 ) {
           return $query->row();
         } else {
            return false;
         }


}

in controller
Code:
$data['department'] = $this->department_model->get_by_id($id);
$this->load->view('dept_info', $data);

in view
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php if(!$department){
  
echo 'department data not available';
}
else
{
?&gt;



<h3 align="center">Business Impact Analysis(BIA) Questionnaire</h3>
<h5 align="center">&lt;?php echo $department->DepartmentName; ?&gt;</h5>
















&lt;?php } ?&gt;


&lt;/body&gt;
&lt;/html&gt;