Within CodeIgniter, I’m doing a select, retrieving some blogposts. But I also want the total reactions of a blogpost with the count() function. But when I’m doing this, it only returns one record.
Example:
$this->db->select('idee_id, titel, omschrijving, type, top_idee, tbl_users.foto as user_foto');
$this->db->from('tbl_idee');
$this->db->join('tbl_types', 'tbl_idee.type_id = tbl_types.type_id');
$this->db->join('tbl_users', 'tbl_idee.user_id = tbl_users.user_id');
$this->db->join('tbl_comments', 'tbl_users.user_id = tbl_comments.user_id');
$this->db->order_by('post_datum', 'DESC');
$this->db->limit(10);
$q = $this->db->get();
Now I only get the blogposts without total reactions of one blogpost.
How can I fix this? With a select in a select.?
Thanks
First you need to make the database select the number of each blog post’s comments
note the second parameter, it prevents codeigniter from parsing the
COUNT()functionyou are retrieving data from a blog post together with its comments, so you need to
GROUP BYthe data which makes a blogpost unique:the rest of your query should be correct