CodeIgniter: help with this query

Posted by iamjonesy on Stack Overflow See other posts from Stack Overflow or by iamjonesy
Published on 2011-01-08T00:05:02Z Indexed on 2011/01/08 0:54 UTC
Read the original article Hit count: 178

Filed under:
|
|

Hi,

I have just started my first CI app. I have a view that displays some posts. Each post can have multiple comments and I want to display the total number of comments next to each post.

So far all my db call are in my controller (will be changing this).

function index(){
    $data['query'] = $this->db->get('posts');
    $this->load->view('blog_view', $data);
}

In my view:

<?php foreach($query->result() as $row): 
      <div class="post-box">
          <p><?php echo $row->body; ?><small>&nbsp;added by <?php echo $row->username; ?> on  <?php echo date ('d/m/Y',strtotime($row->created)); ?>&nbsp;<a href="<?php echo base_url(); ?>blog/comments/<?php echo $row->id; ?>"><img src="<?php echo base_url(); ?>images/comments_icon.png" />&nbsp;0</a></small></p>
      </div>
<?php endforeach; ?>

I want to get the total number of comments where comment.post_id = the current record's id. and display it next to the comments icon.

Any help with this most appreciated,

Billy

© Stack Overflow or respective owner

Related posts about php

Related posts about mvc