Image shows all of my posts where i want to show all comments for each post.i marked the comments section with the red color.`
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);
}
The above code is controller part. and the following code for model to retrieve all posts from database.
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();
}
the following code for comments in model to retrieve comment from database
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();
}
the view code part
<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 }?>
the above code problem is it retrieves all comments from database.i don't want it.i want all comments for each post.how to write the query to retrieve data for each post.
</div>
Do this: past post_id
in get_comments_by_post
function
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 '';
}
}