Display CodeIgniter query results in separate sections of an HTML table when a grouping value changes
06:34 05 Aug 2014

I can extract a set of results from a table like this:

$this->db->select('sets.id,
                   sets.wo_id,
                   sets.weight,
                   sets.reps,
                   exercise_list.title');
$this->db->from('sets');
$this->db->join('exercise_list', 'sets.ex_id= exercise_list.id');
$this->db->where('sets.wo_id', $wo_id);
//return $query->result_array();
$q = $this->db->get();
$query = $q->result_array();
return $query;

Without worrying too much about what that is doing, it returns me an array (only showing the bits I use) like this:

******************************
* title  *  weight  *  reps  *
******************************
* exer1  *  25      *   6    *
* exer1  *  25      *   5    *
* exer1  *  25      *   5    *
* exer3  *  80      *   7    *
* exer3  *  80      *   7    *
* exer3  *  80      *   6    *
******************************

What I currently do is just foreach loop thru this table to present them:


  
    
    
    
  

Which renders, very similarly to the mysql array showing above.

BUT what I want to achieve is something more like this (example only):

Set No.WeightReps 1 25 6 1 25 5 1 25 5
Set No.WeightReps 1 80 7 1 80 7 1 80 6

So it renders something like this:

***************************
*         Exer1           *
***************************
* Set No. * Weight * Reps *
***************************
*    1    *   25   *  6   *
*    2    *   25   *  5   *
*    3    *   25   *  5   *
***************************

***************************
*         Exer2           *
***************************
* Set No. * Weight * Reps *
***************************
*    1    *   80   *  7   *
*    2    *   80   *  7   *
*    3    *   80   *  6   *
***************************

Sorry this is such a long post, hopefully its clear what I am trying to do. I think it is something along the lines of as I get the data out of mysql, iterate through it row by row and add it to sub-arrays per exer*.

So I should get an array something like this:

array
(
  exer1
  (
    array(25,6), array(25,5), array(25,5)
  )
  exer2
  (
    array(80,7), array(80,6), array(80,6)
  )
)
php arrays codeigniter multidimensional-array grouping