图片显示了我想要显示每个帖子的所有评论的所有帖子。我用红色标记了评论部分。
public function forum(){
$config['base_url'] = site_url('tourism_con/forum');
$config['total_rows'] = $this->db->get('forum_posts')->num_rows();
$config['uri_segment'] = 3;
$config['per_page'] = 5;
$config['full_tag_open'] = "<ul class='pagination pagination-lg'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = "<li>";
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$data['forum_title']='Forum :: Home Page';
$data['forumPosts']='forumPosts';
$data['forum_details']= $this->tourism_model->get_forum_post($config);
$data['comment_count']=$this->tourism_model->get_comments_by_post();
$this->load->view('Forum/forum_home',$data);
}
以上代码是控制器部分。 和以下代码模型从数据库中检索所有帖子。
public function get_forum_post($config){
$this->pagination->initialize($config);
$this->db->order_by("post_id", "desc");
$query = $this->db->get('forum_posts',$config['per_page'],$this->uri->segment(3));
return $query->result();
}
以下代码用于模型中的注释以从数据库中检索注释
public function get_comments_by_post(){
$this->db->select('comment_id');
$this->db->distinct();
$this->db->from('comment');
$query = $this->db->get();
return $query->num_rows();
}
视图代码部分
<div class='container forumBG'>
<?php
foreach ($forum_details as $forum_post_details) {
?>
<div class="row">
<div class="forumPosts col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-1 ">
<h3 autofocus='autofocus'><strong > <a href="<?php echo site_url()?>tourism_con/full_post_details/?post_id=<?php echo $forum_post_details->post_id?>"> <?php echo $forum_post_details->topic_title?></a></strong></h3>
Written By:<strong><?php echo $forum_post_details->user_name?></strong><br/>
Posted on <strong><?php echo $forum_post_details->written_time?></strong>
<p><?php echo substr($forum_post_details->description,0,105) ;?> ...</p>
</div>
<div class="col-xs-10 col-xs-offset-1 col-sm-2 col-sm-offset-0 forumComment">
<div class="row">
<div class="col-sm-12 forumCommentSub">
<i class="fa fa-comments fa-1x"> <?php echo $comment_count;?></i>
</div>
<div class="col-sm-12 forumCommentSub">
<i class="fa fa-eye fa-1x"> <?php echo $forum_post_details->post_views?></i>
</div>
</div>
</div>
</div>
<?php }?>
&#13;
上面的代码问题是它检索来自database.i的所有注释。我不想要它。我希望每个post.how的所有注释都可以编写查询来检索每个帖子的数据。
答案 0 :(得分:0)
执行此操作:在post_id
函数
get_comments_by_post
public function get_comments_by_post($postid){
$q = $this->db->query("SELECT * FROM comments WHERE post_id = $postid");
if($q->num_rows() > 0) {
return $q->result_array();
} else {
return '';
}
}