Issue
I have Database like this and want the return like :
id_user : 5c348f8041dc5 have 2
id_user : 5cfc8a7d33a12 have 1
How to do that or the name of the tutorial that I should look for?
Thanks.
Solution
You are looking for a COUNT
and a group_by
statement.
https://www.codeigniter.com/user_guide/database/query_builder.html
function test() {
$this->load->database();
$this->db->select('id_user, COUNT(*) as total');
$this->db->from('test'); // replace 'test' with your database table
$this->db->group_by('id_user');
$q = $this->db->get();
if ($q->num_rows() == 0) {
show_error('no rows');
}
foreach ($q->result() as $item) {
echo "id_user: $item->id_user have $item->total <br>";
}
}
Result:
Answered By - Alex
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.