[eluser]Unknown[/eluser]
Firstly I'm sorry to bring this up again. I'm aware this question has been asked a couple of times before but none of the solutions I've read seem to work for me.
Basically what I'm trying to do is add the number of comments to the "comments" link in a Blog. So it it'd be like:
Code:
Title: Post
Body: Blah Blah
Comments (10)
It's really frustrating because it seems like such a simple problem if not for the separation of logic from the view file.
Right now I have a model that contains this:
Code:
function getEntries()
{
$query['entries'] = $this->db->get('entries');
return $query;
}
getEntries is called in my Blog controller here:
Code:
function blogPosts()
{
$this->load->model('Blog_model');
$data['entries'] = $this->Blog_model->getEntries();
$this->load->view('blog_view', $data);
}
...and finally this is sent off to be displayed in the view here:
Code:
<?php foreach($entries->result() as $row):?>
<?php echo $row->title;?>
<?php echo $row->date;?>
<?php echo $row->body;?>
<?php echo anchor('blog/comments/'.$row->ID, ' Comments');?>
<?php endforeach; ?>
Back when I used to code things in regular PHP I'd of just done a count SQL query for each iteration of the loop (using the row ID to decide which comments are relevant to each individual blog post) but with this new-fangled MVC business I need to already have everything sorted out before I get anywhere near the loop.
I think what I need to do is essentially create a new temporary table containing only entry IDs and a count for the number of comments associated with this entry id. Then if I could join this to the entries table (on the entry id) it would be easy to output the relevant data at the view stage... on the other hand I might be completely wrong.
If anyone could help me I would be extremely grateful.
Thanks.
ADDED: One other thing - here are my table structures:
Entries:
ID
Title
Date
Body
Comments:
ID
entry_ID
Body
Author