Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3676634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:07:22+00:00 2026-05-19T03:07:22+00:00

I have just started my first CI app. I have a view that displays

  • 0

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

UPDATE

Controller:

function index(){
    //load the model
    $this->load->model('City_model');

    //call the model method
    $data->posts = $this->City_model->get_posts();


    $this->load->view('blog_view', $data);
}

Model (city_model.php):

<?php

class City_model extends Model{

    function get_posts($id = NULL) {

        //define optional id for single post
        //if an id was supplied
        if ( $id != NULL ) {
            $this->db->where('id',$id);
        }

        // execute query
        $query = $this->db->get('posts');

        //make sure results exist
        if($query->num_rows() > 0) {
            $posts = $query->result();
        } else {
            return FALSE;
        }

        //create array for appended (with comments) posts
        $appended_posts_array = array();

        //loop through each post
        foreach ($posts as $post) {

            //get comments associated with the post
            $this->db->where('post_id', $post->id)
            $comments = $this->db->get('comments');

            //if there are comments, add the comments to the post object
            if($comments->num_rows() > 0) {
                $post->comments = $comments;
            }
            else {
                $post->comments = array();
            }

            //rebuild the returned posts with their comments
            $appended_posts_array[] = $post;

        }

        //if post id supplied, only return the single post object
        if ($id != NULL) {
            return $appended_registration_array[0];
        }
        else {
            return $appended_registration_array;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:07:23+00:00Added an answer on May 19, 2026 at 3:07 am

    Here is an example model method that takes the approach I usually do for Items that have “sub” items…

    I generally build them in a Multi-level array in the model like so…

    Note: this model returns a post or all posts, complete with an array of associated comments accessable through the ->comments property.

    function get_posts($id = NULL) {
    
        //define optional id for single post
        //if an id was supplied
        if ( $id != NULL ) {
            $this->db->where('id',$id);
        }
    
        // execute query
        $query = $this->db->get('posts');
    
        //make sure results exist
        if($query->num_rows() > 0) {
            $posts = $query->result();
        } else {
            return FALSE;
        }
    
        //create array for appended (with comments) posts
        $appended_posts_array = array();
    
        //loop through each post
        foreach ($posts as $post) {
    
            //get comments associated with the post
            $this->db->where('post_id', $post->id)
            $comments = $this->db->get('comments');
    
            //if there are comments, add the comments to the post object
            if($comments->num_rows() > 0) {
                $post->comments = $comments;
            }
            else {
                $post->comments = array();
            }
    
            //rebuild the returned posts with their comments
            $appended_posts_array[] = $post;
    
        }
    
        //if post id supplied, only return the single post object
        if ($id != NULL) {
            return $appended_registration_array[0];
        }
        else {
            return $appended_registration_array;
        }       
    }
    

    Now in the controller…

    function posts() {
    
        //load the model
        $this->load->model('model_name');
    
        //call the model method
        $data->posts = $this->model_name->get_posts();
    
        //load the view
        $this->load->view('view/file', $data);
    
    }
    

    Then in the view you can use a nested foreach to loop through the posts AND the comments

    <? foreach($posts as $post): ?>                //posts foreach start
    
        <h1><?= $post->title ?></h1>  //post title
        <p><?= $post->body ?></p>     //post body
    
        <? foreach($post->comments as $comment): ?>     //comments foreach start       
            <h3><?= $comment->author ?></h3>  //comment author
            <p><?= $comment->body ?></h3>     //comment body
        <? endforeach; ?>                               // end comments foreach
    
    <? endforeach; ?>        // end posts foreach
    

    Take note that once you build the posts array like I have shown in the model, for each $post item you have a $post->comments that is simply an array of comments that are associated with that post, so knowing that, you can call count($post->comments) in the controller or view to get the number of comments related to a single post.

    So for your question about just displaying the count, in the view the only change is that you wouldn’t loop through all the comments, you’d just do this instead…

    <? foreach($posts as $post): ?>                //posts foreach start
    
        <h1><?= $post->title ?></h1>             //post title
        <p><?= count($post->comments) ?></p>     //comment count
    
    
    <? endforeach; ?>        // end posts foreach
    

    EDIT :
    I added the optional parameter $id to the model method so that if you want you can specify a single post if you wish, by passing it’s id to the method. That way this same model method can be re-used to display single posts in detail with all comments displayed also.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started trying to build my first wp7 app, and I am
I have just started building my first Flask app, which currently simply returns output
I've just started with my first real rails app. I have a predefined set
I have just started the Assembly language programming and in the first lecture our
I have just started learning Erlang and am trying out some Project Euler problems
I have just started migrating my homegrown persistence framework to JPA. Given that the
I have just started working on my first Android application and am going ok.
Firstly, great job on B4A. I've just started, and already have a small app
I just started doing my first Android app which happens to be an RSS
I have just started having a go at developing a small KDE app as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.