Php 如何显示总评论

Php 如何显示总评论,php,database,codeigniter,Php,Database,Codeigniter,Whatsup codeigniters! 我想显示我用codeigniter构建的博客的总评论。 在我的控制器中,我有: function index() { $data['query'] = $this->blog_model->get_all_entries(); $this->load->view('blog/index',$data); } 函数index()获取所有帖子。 我有 public func

Whatsup codeigniters! 我想显示我用codeigniter构建的博客的总评论。 在我的控制器中,我有:

   function index() {

          $data['query'] = $this->blog_model->get_all_entries();
          $this->load->view('blog/index',$data);
   }
函数index()获取所有帖子。 我有

  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();
   }

基本上,post($id)获取一个带有id的post(单篇post)并显示评论。我可以在一篇文章中打印评论总数。但如何在index()函数中打印所有帖子的总评论数呢。谢谢大家!

试着做这样的事情 模型中

内部控制器

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

为此,您必须在视图中调用模型。例如,如果要显示帖子名称

视图中的负载模型

  foreach ($all_comments as $row)
        {
            echo $row->post;
          echo $total_no_post = $this->model_name->fetch_total_no_of_comment_in_post($row->postid);
        }
此函数中的注释计数获取\u post中的\u comment\u总数\u数量

使用此活动记录

$this->db->select("post_id , count(comment) as total_comments");    
$this->db->group_by('post_id');
$query = $this->db->get('comments');
这将生成此sql查询

SELECT 
     post_id,
     count(comment) as total_comments
FROM comments
GROUP BY post_id
这意味着选择帖子,计算每个评论的帖子数量,并按帖子分开。为了便于理解,这里是表结构

Comments
id    count(comment)    post_id    

现在,查询将首先获取所有评论,然后使用group by将提供每个帖子总评论的帖子分开。

您想要博客中的总评论还是每个帖子的评论Hi pramod我想要在每个帖子下显示总评论这显示评论,我明白,但我需要在每个标题下显示总评论数post@NewKid我已经更新了我的答案,请检查它是否有用让我试试,我会很快告诉你这是有效的,但我在循环视图中遇到了问题:()

还没有条目是的!它一起返回3711个数字,而不是在post1 3、post2 7、post3 1、post4 1下
Comments
id    count(comment)    post_id