Welcome Guest, Not a member yet? Register   Sign In
Trying to get property of non-object - Need Help


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

function musteri_mesajlari()

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

The related view

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

Thanks in advance.

Hımm. I fixed the foreach statement as below and the problem was solved.

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

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

You are right. Thank you. I will change it on that way.

Theme © iAndrew 2016 - Forum software by © MyBB