如何显示总评论

时间:2012-09-03 09:01:27

标签: php database codeigniter

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(单个帖子)的帖子并显示评论。我可以在单个帖子中打印总评论数。但是如何在index()函数中打印总评论数,其中列出了所有帖子。谢谢!

2 个答案:

答案 0 :(得分:0)

尝试做这样的事情 在模型中

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

在控制器

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

此功能中没有评论任何评论 fetch_total_no_of_comment_in_post

答案 1 :(得分:0)

使用此活动记录

$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    

现在查询将首先获取所有评论,然后它将使用分组来分隔帖子,为每个帖子提供总评论。

相关问题