简体   繁体   中英

How to display total comments

Whatsup codeigniters! I want to display total comments of my blog that I am building with codeigniter. In my controller I have:

   function index() {

          $data['query'] = $this->blog_model->get_all_entries();
          $this->load->view('blog/index',$data);
   }

Function index() gets all posts. and I have

  public function post($id) {
          $data['query'] = $this->blog_model->get_post($id);
          $data['comments'] = $this->blog_model->get_post_comment($id);
          $data['post_id'] = $id;
          $data['total_comments'] = $this->blog_model->total_comments($id);
          $this->load->view('blog/index',$data,TRUE);

          $this->load->helper('form');
          $this->load->library(array('form_validation','session'));
          //validation rules for post function

          $this->form_validation->set_rules('commentor','Name','required');
          $this->form_validation->set_rules('email','Your email','required|valid_email');
          $this->form_validation->set_rules('comment','Comment','required');

          if($this->blog_model->get_post($id))
          {
                 foreach($this->blog_model->get_post($id) as $row) 
                 {
                        //set page title
                        $data['title'] = $row->entry_name;
                 }
                 if($this->form_validation->run() == FALSE)
                 {
                        //if validation runs FALSE
                        $this->load->view('blog/post',$data);
                 }
                 else
                 {
                        //if valid
                        $name = $this->input->post('commentor');
                        $email = strtolower($this->input->post('email'));
                        $comment = $this->input->post('comment');
                        $post_id = $id;

                        $this->blog_model->add_new_comment($post_id,$name,$email,$comment);
                        $this->session->set_flashdata('message', '1 new comment added!');
                        redirect('blog/post/'.$id);
                  }
           }
           else
                  show_404();
   }

Basically, post($id) gets a post with id (single post) and display comments. I can print total comments number in single post. But how do i print total comments number in index() function where all posts are listed. Thank you!

Try to do something like this in Model

public function fetch_all_comment()
{
      $query = $this->db->get('comments');
      return $query->result();
}

In Controller

$data['all_comments'] = $this->model_name->fetch_all_comment();
 $this->load->view('viewname',$data);

In View

For this you have to call model in your view. for example if you want to display post name.

Load Model in view

  foreach ($all_comments as $row)
        {
            echo $row->post;
          echo $total_no_post = $this->model_name->fetch_total_no_of_comment_in_post($row->postid);
        }

Count no of comment in this function fetch_total_no_of_comment_in_post

Use this active record

$this->db->select("post_id , count(comment) as total_comments");    
$this->db->group_by('post_id');
$query = $this->db->get('comments');

This generate this sql query

SELECT 
     post_id,
     count(comment) as total_comments
FROM comments
GROUP BY post_id

Means this selects posts , count of posts for each comment and seperate them by post. For understanding Here is the table Structure

Comments
id    count(comment)    post_id    

Now the query will first fetch all the comments then it will use group by to seperate posts giving you total comments for each post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM