Welcome Guest, Not a member yet? Register   Sign In
How to get different user info when logged in.
#1

(This post was last modified: 01-23-2018, 04:48 PM by kirasiris.)

Hello I'm working in a comment section in which I need to get the info of the current logged in user who commented in a specific post.
So far I've been able to display their username and what they just commented but I would like to display the avatar_img of each user.

This is what I currently have:

[Image: Capture.jpg]

As you can see in the image above, I can display their username and the comment according to what user commmented. Now the problem is how to avoid the duplicate and how to show properly show their info.

To make it more clear, username 'kirasiris' has my personal picture while username 'cibra10' has assassin creed picture and in the picture above both usernama are wrongfully displaying their avatar_img(and the comments are duplicating themselves also).

This is the show method in my Post_controller:

PHP Code:
   public function show($slug)
 
   {
        
// Get Posts by Slug
        
$data['posts'] = $this->Post_model->get_by_slug($slug);
        
        
// Get Comments per Post
        
$post_id $data['posts']->id;
        
$data['comments'] = $this->Post_model->get_comments($post_id);
        
        
// If empty show a 404 error
        
if(empty($data['posts'])){
            
show_404();    
        }

 
       // Load template
 
       $this->template->load('public''default''posts/show'$data);
 
   

This is my get_comments method in my Post_model which allows me to get the comments according to the post.id that is being read or visited:

PHP Code:
// Comments
    
public function get_comments($post_id){
            
 
       $this->db->select('*');
        
$this->db->from('users');// here is where I try to get user info
        
$query $this->db->get_where('comments', array('post_id' => $post_id));
        
        if(
$query->num_rows() > 0)    {
            return 
$query->result_array();
        }
    } 

and here is my view(show.php) in which I need them to be displyed:

PHP Code:
       <!-- Comments -->
 
       <?php if($comments) : ?>
        <?php foreach($comments as $comment) : ?>
        <div class="box-comments box-footer">
            <div class="box-comment">
                <!-- User image -->
                <img class="img-circle img-sm" src="<?php if($comment['avatar_img']) : echo $comment['avatar_img']; else : echo base_url().'assets/img/noavatar.jpg'; endif ?>" alt="User Image">
                <div class="comment-text">
                <span class="username"><?php echo $comment['username'?><span class="text-muted pull-right"><?php echo $comment['create_date']; ?></span></span>
                <!-- /.username -->
                <p><?php echo $comment['body'?></p>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
        <?php else : ?>
            <div class="alert alert-danger">No comments</div>
        <?php endif; ?>   

I'm not sure if I could explain myself but I had to do the effort to create something like an Social Network.
I do Front-End development most of the time 
Reply


Messages In This Thread
How to get different user info when logged in. - by kirasiris - 01-22-2018, 07:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB