Jump to content

Displaying posts and comments on a single page using codeigniter


Youngn
Go to solution Solved by Youngn,

Recommended Posts

I have an issue regarding user posts and comments.

I can retrieve posts and comments which is alright but the comments don't seem to align with corresponding posts.

Please see code and image attached.

 

Post Model

public function get_user_posts(){
$session = $this->session->userdata('user_id');
$this->db->select('*');
$this->db->from('posts');
$this->db->join('users', 'users.user_id = posts.user_id');
$this->db->where(array('posts.user_id' => $session));
$this->db->order_by('date_created', 'DESC');
$query = $this->db->get();

return $query->result_array();

}

public function get_comments(){
$session = $this->session->userdata('user_id');
$this->db->select('*');
$this->db->from('post_comment');
$this->db->join('posts', 'posts.post_id = post_comment.post_id');
$this->db->join('users', 'users.user_id = post_comment.author');
$query = $this->db->get();
return $query->result_array();
}

Controller

$data['posts'] = $this->Post_model->get_user_posts();
$data['post_comments'] = $this->Post_model->get_comments();

View

<div class="col-lg-5" id ="post_box" style="height: 600px !important; overflow: auto;">
    <?php
    foreach($posts as $post):                                  
    if($post['media_img'] != "")
    {
    $img = $post['media_img'];
    }
    else
    {
    $img = "blank_user.png";
    }
    ?>
    <div class="social-feed-box">                       
    <div class="pull-right social-action dropdown">                            
    </div>
    <div class="social-avatar">
    <a href="" class="pull-left">
    <img alt="image" src="<?php echo base_url();?>/public/user_img/<?php echo $img;?>">                                
    </a>
    <div class="media-body">
    <a href="#">
    </a>
    <small class="text-muted"><?php echo dateFormat('d/m/Y h:ia',$post['date_created']);?></small>
    </div>
    </div>                      
    <div class="social-body">
    <p>
    <?php echo $post['entry_text'];?>
    </p>
    <div class="btn-group">
    <button class="btn btn-white btn-xs"><i class="fa fa-thumbs-up"></i> Like this!</button>
    <button class="btn btn-white btn-xs" onclick="commentClick();" id="comment_btn"><i class="fa fa-comments"></i> Comment</button>
    <button class="btn btn-white btn-xs"><i class="fa fa-share"></i> Share</button>
    </div>
    </div>
    <div class="social-footer" id="comment_section" >
    <?php
    // if(!empty($post_comments)):
    foreach($post_comments as $comments):
    if(!in_array($comments, $post )):
    if($comments['media_img'] != "")
    {
    $img = $comments['media_img'];
    }
    else
    {
    $img = "blank_user.png";
    }
    ?>
    <div class="social-comment">
    <a href="" class="pull-left">
    <img alt="image" src="<?php echo base_url();?>/public/user_img/<?php echo $img;?>">
    </a>                             
    <div class="media-body">
    <a href="#">
    <?php echo $comments['username'];?>
    </a>
    <?php echo $comments['txt'];?>
    <br/>
    <small class="text-muted">12.06.2014</small>
    </div>                               
    </div>
    <?php endif; endforeach; ?>
    <div class="social-comment">                               
    <div class="media-body">
    <textarea class="form-control" placeholder="Write comment..."></textarea>
    </div>
    </div>
    
    </div>       
    </div>
    <?php
                                    
    endforeach;
    ?>
    </div>

Please see image attached for result

 

 

post-205630-0-19015100-1519741334_thumb.png

Link to comment
Share on other sites

$data['post_comments'] = $this->Post_model->get_comments();
Process those comments and figure out which ones belong to which post. For example, make an array with the post ID as the key and the array of comments as the value. Then you wouldn't loop over the post_comments itself but the comments for the particular post you're on - if there are any comments for it, of course.
Link to comment
Share on other sites

Are you really planning to show ALL posts and ALL comments on a single page? What happens when you have hundreds/thousands of posts/comments?

 

At some point you are going to want to limit the posts based on date (or some other filter). Taking that into account you would only want to get the comments respective to those posts.

 

So, you need to take a different approach. You should run ONE query to get the posts and the comments in a single result set. I don't use whatever framework you are using, but the query would look something like this

 

 

SELECT post.media_img as post_media_img, post.date_created as post_date_created, post.entry_text,
       post_user.username as post_username,
       comment.media_img as comment_media_img, comment.date_created as comment_date_created, comment.txt,
       comment_user.username as comment_username,
 
FROM posts
JOIN users post_user ON post_user.user_id = posts.user_id
JOIN post_comment comment ON comment.post_id = posts.post_id
JOIN users comment_user ON comment_user.user_id = comment.author
 
ORDER BY posts.date_created DESC,
         comments.date_created DESC
Link to comment
Share on other sites

The way you are doing this is problematic because you have one function that returns your list of Posts, and one that returns your list of comments.

 

Your controller needs more sophistication.

 

The get_comments() function needs to have a parameter of $post_id that gets passed to it:

 

public function get_comments($post_id){
  
  $this->db->select('*');
  $this->db->from('post_comment');
  $this->db->join('users', 'users.user_id = post_comment.author');
  $this->db->where(array('post_comment.post_id' => $post_id));
  $query = $this->db->get();
  return $query->result_array();
}
In your controller you need to add the comments array to each individual post, and adjust your view code accordingly.

 

 

// Controller
$data['posts'] = $this->Post_model->get_user_posts();
foreach($data['posts'] as $post)  {
    $post['comments'] = $this->Post_model->get_comments($post['post_id']);
}
  • Like 1
Link to comment
Share on other sites

That is a poor solution. You are pulling EVERY comment, and then filtering them in the view. As more comments get added to the system, your code will degrade and become slower and slower. You are also adding load to your database requiring it to scan the comments table repeatedly when you only want to comments related to the posts for a specific user. You should be using either the suggestion made by Psycho or the one I provided.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.