PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] How To Count Every User And Return Array In Codeigniter

 January 30, 2022     codeigniter, database, php     No comments   

Issue

Image Database here

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:

enter image description here



Answered By - Alex
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing