Codeigniter显示已删除用户的评论


Codeigniter show comments of deleted users

下面的代码帮助我显示特定帖子的所有评论(包括用户信息)。

当用户停用他的帐户时,我希望他的评论对其他用户仍然可见,但不显示他的信息(如姓名和城市)。我想显示默认值。

所以我需要修改下面的代码,以便检查用户是否存在。如果没有,则返回默认的名称和城市值。

我花了几个小时试图找到一个解决方案,但没有得到我想要的结果。你能帮我解决这个问题吗。

public function get_comments($post_id){
$this->db->select('comments.comment_text, users.name,users.city');
$this->db->from('comments','users');   
$this->db->where('comments.post_id', $post_id);
$this->db->join('users','comments.user_id = users.user_id');    
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
     }
}

您可以检索所有用户信息,并在检查用户是否处于活动状态后:

$data = $this->db->select('comments.comment_text, users.name, users.city, users.active');
->from('comments');
->where('comments.post_id', $post_id);
->join('users','comments.user_id = users.user_id')
->get()result();

然后在你的视图中,你检查你的用户是否活跃,如果是这种情况,你将显示所有信息,如果你只显示你想要的:

foreach($data as $d) 
{
  if($d->active == 1) // User active
  {
    echo .....;
  }
  else // User not active
  {
    echo ...;
  }
}