2014-10-11 69 views
1

從'評論'表中刪除評論時,我想減少'posts'表中的評論數量。你能否檢查下面的代碼,並幫我獲取$ post_id的值。我使用Codeigniter。Codeigniter刪除評論並減少評論數量

這裏是 '意見' 表:

enter image description here

public function remove_comment($comment_id) 
    { 
     $this->db->where('id', $comment_id); 
     $query = $this->db->get('comments'); 

     if ($query->num_rows() == 1) { 

      $this->db->where('id', $comment_id); 
      $this->db->delete('reviews'); 

      $post_id = // grab the value from 'comments' table 

      $this->db->where('post_id', $post_id); 
      $this->db->set('comments', 'comments - 1', FALSE); 
      $this->db->update('posts'); 

      return true; 
     } else { 
      return false; 
     } 
    } 

回答

1

那麼您需要首先獲取它,你可以使用->row()

$query = $this->db->get('comments'); 
$result = $query->row(); 
$post_id = $result->post_id;