title: How to display data from query - one to many relationship between db tables
body: <p>I'm trying to display data from query but I can't show it as it should.
  My tables in database are: <code>ordersheader</code> and <code>orderitems</code>. It's one to many relationship between them. One ordersheader have many orderitems. They are joined ON idOrder. One idOrder in <code>ordersheader</code> corresponds to many rows in <code>orderitems</code>.
  I have to show all orders for current date and to show all orderitems for each idOrder. It should look like this for all orders:</p>
  
  <p><a href="http://prntscr.com/7lwoan" rel="nofollow">http://prntscr.com/7lwoan</a></p>
  
  <p>First row is order - it has to be displayed all orderitems for this order and when you click <code>Details</code> it has to be shown each orderitem on separate row.
  Now, as I used code below, each orderitem is shown on separate row. How to make it looks like the above link? Thanks!
  My model is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> function getOrders(){
      $date = new DateTime("now");
   
      $curr_date = $date-&gt;format('Y-m-d ');
      $this-&gt;db-&gt;select('ordersheader.*,customer.name,orderitems.*');
      $this-&gt;db-&gt;from('ordersheader'); 
      $this-&gt;db-&gt;join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
      $this-&gt;db-&gt;join('customer', 'customer.idCustomer = ordersheader.idCustomer');
      $this-&gt;db-&gt;where('DATE(orderDueDate)', $curr_date);   
  
      $query = $this-&gt;db-&gt;get();
      return $query-&gt;result();
      
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>My view is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
   if($orderlist){
  foreach($orderlist as $row) {
   ?&gt; &lt;tr class="overview"&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;idOrder; ?&gt;&lt;/td&gt;
                          &lt;td class="text-center"&gt;&lt;img src="&lt;?= base_url();?&gt;/assets/images/tick.png"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;name; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;eggSize; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;quantity; ?&gt;&lt;/td&gt;
                          &lt;td&gt;
                              &lt;div class="progress"&gt;
                                  &lt;div class="progress-bar" role="progressbar" style="width: 60%;"&gt;
                                      60%
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/td&gt;
                          &lt;td&gt;18:00&lt;/td&gt;
                          &lt;td&gt;&lt;button type="button" class="btn btn-link btn-xs view-full-order"&gt;Full&lt;/button&gt;&lt;/td&gt;
                      &lt;/tr&gt; 
                      &lt;tr class="full hide"&gt;
                          &lt;td&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;eggSize; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;quantity; ?&gt;&lt;/td&gt;
                          &lt;td&gt;
                              &lt;div class="progress"&gt;
                                  &lt;div class="progress-bar progress-bar-info" role="progressbar" style="width: &lt;?php echo $rand; ?&gt;%;"&gt;
                                    &lt;?php echo $rand; ?&gt;%  
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/td&gt;
                          &lt;td&gt;14:00&lt;/td&gt;
                          &lt;td&gt;&lt;/td&gt;
                      &lt;/tr&gt;
   &lt;?php } } ?&gt;
               &lt;/tbody&gt;&lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Edited: I made it with group_concat and explode. But now, problem is that for each idOrder I should calculate average rand (this is the value of progress bar) of all orderitems. This should be for each order - avegare sum of orderitems. Now, it's not calculated correct. 
  It now looks like that:</p>
  
  <p><a href="http://prntscr.com/7mu0k3" rel="nofollow">http://prntscr.com/7mu0k3</a>
  This row where is Billa should be average of the next 2 rows.
  My view is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;tbody class="client"&gt;
              &lt;?php 
              if ($orderlist) {
                  foreach ($orderlist as $row) {
                      $i = 0;
  
                      ?&gt; &lt;tr class="overview"&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;idOrder; ?&gt;&lt;/td&gt;
                          &lt;td class="text-center"&gt;
                              &lt;?php
                               $s = $row-&gt;eggSize; 
                      $egg_size_array = explode(",", $row-&gt;eggSize);
                      $quantity_array = explode(",", $row-&gt;quantity);
                      
                      if (!empty($egg_size_array))
                          foreach ($egg_size_array as $key =&gt; $value) {
                              $rand[$key] = rand(1, 100);
                              $random_hour[$key] = rand(01, 24);  
                            
                          }
  ?&gt;                      
                              
                          &lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;name; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $row-&gt;eggSize; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php
                              $s = $row-&gt;quantity;
                              $s = explode(',', $s);
                              echo array_sum($s);
                           print_r($rand);  echo "&lt;br&gt;";print_r(array_sum($rand)); echo "&lt;br&gt;";   print_r(count($egg_size_array));
                              ?&gt;&lt;/td&gt;
                          &lt;td&gt;
                              &lt;div class="progress"&gt;
                              
                                  &lt;div class="progress-bar" role="progressbar" style="width: &lt;?php 
                                  echo array_sum($rand)/ count($egg_size_array); ?&gt;%;"&gt;
                                       
                                        
                                  &lt;?php  echo array_sum($rand)/ count($egg_size_array); echo "%";  ?&gt;
  
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/td&gt;
                          &lt;td&gt;&lt;?php echo max($random_hour); echo " ч."; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;button type="button" class="btn btn-link btn-xs view-full-order"&gt;
                      &lt;?php echo lang('details'); ?&gt;  &lt;/button&gt;&lt;/td&gt;
                      &lt;/tr&gt; 
                      &lt;?php
                     
                      if (!empty($egg_size_array))
                          foreach ($egg_size_array as $key =&gt; $value) {
                              // $rand = rand(1, 100);
                              //$random_hour = rand(01, 24);
                              ?&gt;
                              &lt;tr class="full hide"&gt;
                                  &lt;td&gt;&lt;/td&gt;
                                  &lt;td&gt;&lt;/td&gt;
                                  &lt;td&gt;&lt;/td&gt;
                                  &lt;td&gt; 
                  &lt;?php echo $value; ?&gt;&lt;/td&gt;
                                  &lt;td&gt;&lt;?php echo $quantity_array[$key]; ?&gt;&lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;div class="progress"&gt;
                                          &lt;div class="progress-bar progress-bar-info" role="progressbar" style="width: &lt;?php echo $rand[$key]; ?&gt;%;"&gt;
                  &lt;?php echo $rand[$key]; ?&gt;%  
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/td&gt;
                                  &lt;td&gt;&lt;?php echo $random_hour[$key] . ' ч.' ?&gt;&lt;/td&gt;
                                  &lt;td&gt;&lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;?php
                          }
                      ?&gt;&lt;?php
                  }
              }
              ?&gt;
          &lt;/tbody&gt;&lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>My model is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  function getOrders(){
          $date = new DateTime("now");
          $curr_date = $date-&gt;format('Y-m-d');
          $this-&gt;db-&gt;select('orderitems.eggSize as size');
          $this-&gt;db-&gt;select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.itemNumber) as itemNumber');
          $this-&gt;db-&gt;select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.quantity ) as quantity ');
          $this-&gt;db-&gt;select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.unitPrice) as unitPrice');
          $this-&gt;db-&gt;select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.eggSize ) as eggSize');
          $this-&gt;db-&gt;from('ordersheader'); 
          $this-&gt;db-&gt;join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
          $this-&gt;db-&gt;join('customer', 'customer.idCustomer = ordersheader.idCustomer');
          $this-&gt;db-&gt;where('DATE(orderDueDate)', $curr_date);
          $this-&gt;db-&gt;group_by('orderitems.idOrder');
          $query = $this-&gt;db-&gt;get();
          return $query-&gt;result();
      }</code></pre>
  </div>
  </div>
  </p>