CodeIgniter Forums

Full Version: How to group product by category?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've a project where i've to show content by group, now i not only have to group them and show total, i've to group them and show all content inside it. My Table looks like this

-----------------------------------------------------------------
   id    |    name       |         role         |         salary
-----------------------------------------------------------------
   1     |      abc        |       dev           |          5k
   2     |      xyz        |       sr. dev      |         7k
   3     |      jkm        |       intern        |         1k
   4     |      asf         |       dev           |         5k
   5     |      asdf       |       mang.       |        10k
   6     |      vas        |       intern        |         1k
   7     |      m.kj       |       dev           |         5k
   8     |      kjnm      |       dev           |         5k
   9     |      knlk       |       sr. dev      |         7k
 10     |      kljf         |       dev           |         5k
The CodeIgniter Active Record Class offers a method called group_by which you can use to query a table with the results representing a grouping of the records in the table.
Code:
$this->db->select("COUNT(id)");
$this->db->group_by("role");
$this->db->get("my_table");

You should probably read the mysql documentation on 'group by'. Or if you are using some other DBMS, check out its documentation on group by.