2014-10-30 58 views
-1

以下代碼可幫助我顯示特定帖子的所有評論(包含用戶信息)。Codeigniter顯示已刪除用戶的評論

當用戶停用其帳戶時,我希望他的評論對其他用戶仍可見,但不顯示他的信息(如姓名和城市)。我想要顯示默認值。

所以我需要修改我的代碼,以便它會檢查用戶是否存在。如果不是,則返回默認名稱和城市值。

我想找到一個解決方案几個小時,但不能得到我想要的結果。你能幫我解決這個問題嗎?

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; 
    } 
} 

回答

2

您可以檢索的所有用戶信息,您檢查後,如果用戶是活躍:

$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 ...; 
    } 
}