• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get property of non-object - Need Help

#1
[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.

#2
[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;
}

#3
[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.

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.