CodeIgniter Forums

Full Version: Get comment count of latest blog entries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I am new to CI, and new to php frameworks in general, though I have other programming experience and worked with php with Wordpress and other APIs.

As a way of learning CI, I am trying to built a blog/cms sort of thing.

On the home page of my application, I display the latest 10 posts as follows:

    $data['entries'] = $this->last_entries(10);
    $this->load->view('home', $data);

and view:
    <?php foreach($entries as $entry): ?>
        &lt;header&gt;&lt;h3>&lt;?php echo $entry->title; ?&gt;</h3>&lt;/header&gt;
        &lt;?php echo $entry->body; ?&gt;
        <footer><p>Posted on &lt;?php echo $entry->datetime; ?&gt;</p></footer>
    &lt;?php endforeach; ?&gt;

However, I'd also like to include the comment count in the footer of each post. I have a model function that can get the the count based on the post id. However, because each individual post is not rendered until the view, it seems like I would have to load the model into the view in order to do that.

From what else I've found on this forum, that seems to be frowned upon, and moreoever I'm not sure how to load the model into the view..should it be done before the foreach loop? or maybe for each individual entry?

This is what I'd like to be able to do for each entry:
    <footer><p>Posted on &lt;?php echo $entry->datetime; ?&gt; - &lt;?php echo count($this->comments_from_post($entry->id)); ?&gt; comments</p></footer>

So, what would be the best way for achieving this?