Welcome Guest, Not a member yet? Register   Sign In
Query with Null Result
#1

[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


Messages In This Thread
Query with Null Result - by El Forum - 04-25-2010, 09:10 PM
Query with Null Result - by El Forum - 04-25-2010, 09:24 PM
Query with Null Result - by El Forum - 04-25-2010, 09:28 PM
Query with Null Result - by El Forum - 04-26-2010, 01:18 AM
Query with Null Result - by El Forum - 04-26-2010, 09:57 AM
Query with Null Result - by El Forum - 04-26-2010, 10:29 AM
Query with Null Result - by El Forum - 04-26-2010, 12:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB