• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get different user info when logged in.

#1
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

#2
It depends on what you are returning from your slug method an object or an associate array.

You need to show your get_by_slug method in your model.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(01-23-2018, 05:54 AM)InsiteFX Wrote: It depends on what you are returning from your slug method an object or an associate array.

You need to show your get_by_slug method in your model.

I'm returning an object using $query->row(). Should I change it to result_array()?

PHP Code:
   public function get_by_slug($slug)
 
   {
 
       $this->db->select('*');
 
       $this->db->from($this->table);
 
       $this->db->where('slug'$slug);
 
       $this->db->where('is_published'1);
 
       $this->db->limit(1);

 
       $query $this->db->get();

 
       if ($query->num_rows() == 1) {
 
           return $query->row();
 
       } else {
 
           return false;
 
       }
 
   

But would it be possible to return value from a different table(users) ?
I do Front-End development most of the time 
Reply

#4
Yes, I would return an associated array instead to keep your method all the same.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(01-24-2018, 05:34 AM)InsiteFX Wrote: Yes, I would return an associated array instead to keep your method all the same.

I already changed the $query->row() to $query->result_array() in the get_by_slug method but it now gives me an error of undefined index which comes from the $post_id = $data['posts']->id;

Now if I use $query->row_array() instead of $result_array() it does not gives error and I can see the post clearly with not problem but just by changing the $post_id = $data['posts']->id; to $post_id = $data['posts']['id']; as well.

Here is my new methods in the 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);
 
   

and here my two method on the Post_model:

PHP Code:
   public function get_by_slug($slug)
 
   {
 
       $this->db->select('*');
 
       $this->db->from($this->table);
 
       $this->db->where('slug'$slug);
 
       $this->db->where('is_published'1);
 
       $this->db->limit(1);

 
       $query $this->db->get();

 
       if ($query->num_rows() == 1) {
 
           return $query->row_array();
 
       } else {
 
           return false;
 
       }
 
   }

// Comments
    
public function get_comments($post_id){
            
 
       $this->db->select('*');
        
$this->db->from('users');
        
$query $this->db->get_where('comments', array('post_id' => $post_id));
        
        if(
$query->num_rows() > 0)    {
            return 
$query->result_array();
        } else {
            return 
false;
        }
    } 

is basically the same I still don't know how to show users info and avoid the duplicate of comments. I will show you some screenshot of my three tables posts,comments, users.

Post table:
[Image: Capture.jpg]

Comments table:
[Image: Capture.jpg]

Users table:
[Image: screencapture_localhost_phpmyadmin_tbl_s...363004.png]

Not sure if I could explain myself but thanks for helping I really want to get this done!.

NOTE: The view is stil the same regarding the comment section, the only thing I had to change was the way in which to disply the post title and content from:

$posts->title; to $posts['title']; in order to make it work with the $post_id = $data['posts']['id'];

How can I avoid the duplicate of comments and how can I show the users info in the same view as I would like to add a link-to-profile in the profile picture of each comment.
I do Front-End development most of the time 
Reply

#6
This is the the code that I use to get my Comments.

PHP Code:
/**
 * getComments ()
 * -------------------------------------------------------------------
 *
 * @param  $postId
 * @return array
 */
public function getComments($postId)
{
 
   $data = [];

 
   $this->db->where('post_id'$postId);

 
   $query $this->db->get('comments');

 
   if ($query->num_rows() > 0)
 
   {
 
       foreach ($query->result_array() as $row)
 
       {
 
           $data[] = $row;
 
       }
 
   }

 
   $query->free_result();

 
   return $data;


You can then display them similar to this.

PHP Code:
<h3>Comments</h3>
<?
php
if (count($comments))
{
 
   foreach ($comments as $key => $list)
 
   {
 
       echo "<b>Comment by ".$list['title'].":</b><br>\n";
 
       echo auto_typography($list['body']);
 
   }

 
   echo "<br>";
}
?>

I'am still building my new version, but maybe the above will help.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(01-25-2018, 06:31 AM)InsiteFX Wrote: This is the the code that I use to get my Comments.

PHP Code:
/**
 * getComments ()
 * -------------------------------------------------------------------
 *
 * @param  $postId
 * @return array
 */
public function getComments($postId)
{
 
   $data = [];

 
   $this->db->where('post_id'$postId);

 
   $query $this->db->get('comments');

 
   if ($query->num_rows() > 0)
 
   {
 
       foreach ($query->result_array() as $row)
 
       {
 
           $data[] = $row;
 
       }
 
   }

 
   $query->free_result();

 
   return $data;


You can then display them similar to this.

PHP Code:
<h3>Comments</h3>
<?
php
if (count($comments))
{
 
   foreach ($comments as $key => $list)
 
   {
 
       echo "<b>Comment by ".$list['title'].":</b><br>\n";
 
       echo auto_typography($list['body']);
 
   }

 
   echo "<br>";
}
?>

I'am still building my new version, but maybe the above will help.

I actually got it working, I just edited my comments control a bit and I added an new column in my comments table and I got it working like a charm, now for the user ID link. I just created a helper and this was my result(comments vie new version with user image and link to profile):

PHP Code:
       <?php if($comments) : ?>
        <?php foreach($comments as $comment) : ?>
        <div class="box-comments box-footer">
            <div class="box-comment">
                <!-- User image -->
                <a href="<?php echo base_url() .'public/users/user/'get_user_full_name($comment->user_id); ?>"><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=" <?php echo $comment->username ?> profile image"></a>
                <div class="comment-text">
                <span class="username">
                <a href="<?php echo base_url() .'public/users/user/'get_user_full_name($comment->user_id); ?>"><?php echo $comment->username ?></a><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 do Front-End development most of the time 
Reply

#8
PHP Code:
// You can change this...
<?php echo base_url() .'public/users/user/'get_user_full_name($comment->user_id); ?>

// To this...
<?php echo base_url('public/users/user/'). get_user_full_name($comment->user_id); ?>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.