CodeIgniter Forums
How to group product by category? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to group product by category? (/showthread.php?tid=899)



How to group product by category? - avinashizhere - 01-27-2015

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


RE: How to group product by category? - sneakyimp - 01-28-2015

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.