Welcome Guest, Not a member yet? Register   Sign In
Get comment count of latest blog entries
#1

[eluser]Unknown[/eluser]
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:

controller:
Code:
...
    $this->load->model('blog');
    $data['entries'] = $this->last_entries(10);
    $this->load->view('home', $data);
...

and view:
Code:
...
    <?php foreach($entries as $entry): ?>
    <article>
        &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>
    </article>
    &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:
Code:
...
    <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?




Theme © iAndrew 2016 - Forum software by © MyBB