CodeIgniter Forums

Full Version: Query with Null Result
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]lenwood[/eluser]
Hi All,
I'm trying to get the blog that I'm building to display the comments in the same view as the post they're associated with. If there's at least one comment then everything works, but if the post doesn't have any comments yet then I get an invalid argument. Here's my code.

Controller:
Code:
function post()
{
    $this->load->model('blog_model');
    $this->load->model('comment_model');
    $row = $this->blog_model->single();
    $post_id = $row['id'];
    $body['query'] = $this->comment_model->entry_comments($post_id);
    $body['post_title'] = $row['title'];
    $body['date'] = $row['date'];
    $body['time'] = $row['time'];
    $body['body'] = $row['body'];
    $body['post_id'] = $post_id;
    $this->load->view('post_view', $body);
}

Model:
Code:
function entry_comments($post_id)
{
    $this->db->select('*');
    $this->db->from('comments');
    $this->db->where('entry_id', $post_id);
    
    $query = $this->db->get();
    
    if($query->num_rows() > 0) {
        foreach ($query->result() as $crow) {
            $data[] = $crow;
        }
    return $data;
    }
}

View:
Code:
<h2>&lt;?php echo $post_title; ?&gt;</h2>
<p>&lt;?php echo $date; ?&gt;</p>
<div>&lt;?php echo $body; ?&gt;</div>
<p>&lt;?php echo $time; ?&gt;</p>
<p><strong>Comments:</strong></p>
&lt;?php foreach($query as $row): ?&gt;
    <p>&lt;?php echo $row->body; ?&gt;</p>
    <p>&lt;?php echo $row->author; ?&gt;</p>
    <hr />
&lt;?php endforeach; ?&gt;

If the query returns a null result (no comments for that post yet), the post displays, but where the comment would be I get

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/post_view.php
Line Number: 7

Line 7 in that file is:
Code:
&lt;?php foreach($query as $row): ?&gt;

I've been searching Google and this forum for an hour and I'm coming up empty. Can anyone help?

Thanks in advance,
Chris

El Forum

[eluser]suba[/eluser]
Hi you change in model & view. try my code

Code:
function entry_comments($post_id)
{
    $this->db->select('*');
    $this->db->from('comments');
    $this->db->where('entry_id', $post_id);
    
    $query = $this->db->get();
    return $query;
}

in View
--------
Code:
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
echo $row->body;
echo $body->author;
}
else
{
echo "<div class='error' > No more comments </div>";
}

El Forum

[eluser]Armchair Samurai[/eluser]
Personally, I'd get rid of all the messing around at the end of entry_comments() and just do this:

Code:
$query = $this->db->get();

return $query->result();

If there are no comments, CI will return an empty array, which will allow foreach to iterate. At the moment, you're returning nothing if there are no comments, so foreach is throwing an error.

El Forum

[eluser]paulc010[/eluser]
For a minimal change to arrive at a solution I would use:

Code:
function entry_comments($post_id)
{
    $data = array();

    $this->db->select('*');
    $this->db->from('comments');
    $this->db->where('entry_id', $post_id);
    
    $query = $this->db->get();
    
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $crow) {
            $data[] = $crow;
        }
    }
    return $data;
}

EDIT: You could always free the query result too by adding $query->free_result() before the return Wink

El Forum

[eluser]lenwood[/eluser]
Thanks to all for your replies. I followed Armchair Samurai's example and that took care of it.

This raises another question for me. Can anyone explain when this is needed?

Code:
$query = $this->db->get();

if($query->num_rows() > 0) {
  foreach ($query->result() as $row) {
    $data[] = $row;
  }
  return $data;
}

El Forum

[eluser]paulc010[/eluser]
It certainly isn't in this case, but there may be occasions when you would want to do some post-processing I guess. Copying the data directly as was done in the original doesn't make much sense though.

Paul

El Forum

[eluser]behdesign[/eluser]
[quote author="lenwood" date="1272315467"]Thanks to all for your replies. I followed Armchair Samurai's example and that took care of it.

This raises another question for me. Can anyone explain when this is needed?

Code:
$query = $this->db->get();

if($query->num_rows() > 0) {
  foreach ($query->result() as $row) {
    $data[] = $row;
  }
  return $data;
}
[/quote]

You'd only use that if you wanted to do more with $row than just stick it in an array. As it is, however, you could just
Code:
return $query->result_array();
and get the same result.