CodeIgniter Forums
Trying to get property of non-object - Need Help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Trying to get property of non-object - Need Help (/showthread.php?tid=38397)



Trying to get property of non-object - Need Help - El Forum - 02-07-2011

[eluser]barisv[/eluser]
Hi,

I am trying to show messages that comes from customers to the admin from contact page. But when I'm trying to retrieve data, it gives me an error sth like "trying to get property of non-object".

I couldnt figure out what I doing wrong. When I retrive data by using CI table class, it works just fine, but when I trying to retrieve data one by one, it doesnt work.

The related function of the controller

Code:
function musteri_mesajlari()
    {
        $this->load->library('pagination');
        

        $config['base_url'] = 'http://localhost/adminpanel/admin/musteri_mesajlari';
        $config['total_rows'] = $this->db->get('messages')->num_rows();
        $config['per_page'] = '10';
        
        $this->pagination->initialize($config);
        
        
            $data = array(
            
                'main_content' => 'musteri_mesajlari',
                'username' => $this->session->userdata('username'),
                'records' => $this->db->get('messages', $config['per_page'], $this->uri->segment(3))
            );
        
        $this->load->view('includes/admin_template',$data);
    }

The related view

Code:
<div id="main">
            
            
                &lt;?php if(isset($records)) :    
                    
                            foreach($records as $row)
                              {
                                  echo $row->name;
                             }
                    
                ?&gt;
                
                &lt;?php else : ?&gt;
            
                No records were returned.
                    
                &lt;?php endif; ?&gt;
                    
                
                &lt;?php echo $this->pagination->create_links(); ?&gt;
            
            
        </div>

Thanks in advance.


Trying to get property of non-object - Need Help - El Forum - 02-07-2011

[eluser]barisv[/eluser]
Hımm. I fixed the foreach statement as below and the problem was solved.

foreach($records->result() as $row)
{
echo $row->name;
}


Trying to get property of non-object - Need Help - El Forum - 02-09-2011

[eluser]Dan Bowling[/eluser]
I suggest you look at the basics of MVC again. Generally, you'll want (but not have to) put much of that DB logic in a model and call it from your controller.

I (almost always) have my model return $query->result(), so my view can be left clean.


Trying to get property of non-object - Need Help - El Forum - 02-10-2011

[eluser]barisv[/eluser]
You are right. Thank you. I will change it on that way.