Youngn Posted February 27, 2018 Share Posted February 27, 2018 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted February 27, 2018 Share Posted February 27, 2018 You're retrieving all comments. All of them. Not just the ones that are associated with the particular post you're showing. Quote Link to comment Share on other sites More sharing options...
Youngn Posted February 27, 2018 Author Share Posted February 27, 2018 That's what I thought but I have tried amending the query but still nothing. Do you know a better way please? Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted February 27, 2018 Share Posted February 27, 2018 $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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 27, 2018 Share Posted February 27, 2018 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 Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 27, 2018 Share Posted February 27, 2018 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']); } 1 Quote Link to comment Share on other sites More sharing options...
Youngn Posted February 28, 2018 Author Share Posted February 28, 2018 @gizmola, your method seems like best option for me. I will try this method as soon as I get back on. If I have issues I will get back to you. Thanks mate. Quote Link to comment Share on other sites More sharing options...
Solution Youngn Posted February 28, 2018 Author Solution Share Posted February 28, 2018 I found a way around it. It had to do with my view. I had to add an if statement if post_id = post_comment.post_id retrieve comments of each posts. Thanks everyone. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 1, 2018 Share Posted March 1, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.