EducateYourself
EducateYourself

Reputation: 979

Codeigniter delete comment and reduce comments quantity

When deleting a comment from 'comments' table, I want to reduce the comment quantity in 'posts' table as well. Could you please check my code below and help me to grab the value of $post_id. I use Codeigniter.

here is 'comments' table:

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

Upvotes: 1

Views: 279

Answers (1)

Kevin
Kevin

Reputation: 41903

Well you need to fetch it first, you could use ->row().

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

Upvotes: 1

Related Questions