Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]model->controller->view Error
#1

[eluser]Ducky[/eluser]
Hi,

After exploring CI for a while i decided to start using models in my applications.
So i created a simple model function to return the results as an object.
Everything works fine if there are results found.

I already check the query for results in the model
Code:
if ($query->num_rows() > 0)
    {
    return $query->row();
    }
    else
    {
    return FALSE;
    }

this is how i get the results in the controller
Code:
$data['query'] = $this->Users_model->get_picture($timestamp);
$this->load->view('overview',$data);


This the code on the view page
Code:
<?=$query->picture;?>

the error i receive is(Only when there aren't records to display):
Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/bargain.php

Line Number: 3

I understand i have to check in the "view" if the object exists but i don't find the solution to do this.
It would be great if someone could tell me the best way to achieve this.

Thanks in advance!
Ducky
#2

[eluser]jedd[/eluser]
Your answer first:
Code:
<?php
    if ($query)
        echo $query->picture;
?>

A couple of notes - try to use <?php as it's guaranteed to work on every installation of PHP. Secondly, because you are returning FALSE in the absence of any rows, you can test for truth in the above.

Rather than this:
Code:
if ($query->num_rows() > 0)
    return $query->row();
else
    return FALSE;

You might want to either test for a single row (== 1) or handle multiple rows (return $query->results())
#3

[eluser]Ducky[/eluser]
Thanks for the quick reply and the tip.
I changed what you told me and everything works fine now.
I feel so stupid i couldn't find it myself ...




Theme © iAndrew 2016 - Forum software by © MyBB