title: Split mysql retrieved data into headers and rows
body: <p>I retrieve data from DB and I need to insert all of them in a table.
  I have some constant headers and some that depends on the query results.</p>
  
  <p>Below you can see a table; how i want it to look like:
  <img src="https://i.stack.imgur.com/LEpzK.png" alt="enter image description here"></p>
  
  <p>I did prepare the HTML structure:</p>
  
  <pre><code>&lt;table class="table-fill"&gt;
  &lt;thead&gt;
  &lt;tr&gt;
      &lt;th&gt;ID&lt;/th&gt;
      &lt;th&gt;CODE&lt;/th&gt;
      &lt;th colspan=2&gt;TREND A&lt;/th&gt;      
      &lt;th colspan=2&gt;TREND B&lt;/th&gt;
      &lt;th colspan=2&gt;TREND C&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;Items&lt;/th&gt;
      &lt;th&gt;Revenue&lt;/th&gt;
      &lt;th&gt;Items&lt;/th&gt;
      &lt;th&gt;Revenue&lt;/th&gt;
      &lt;th&gt;Items&lt;/th&gt;
      &lt;th&gt;Revenue&lt;/th&gt;
  &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class="table-hover"&gt;
  &lt;tr&gt;
     &lt;td&gt;1&lt;/td&gt;
     &lt;td&gt;A&lt;/td&gt;
     &lt;td&gt;10&lt;/td&gt;
     &lt;tD&gt;150&lt;/td&gt;
     &lt;td&gt;5&lt;/td&gt;
     &lt;tD&gt;200&lt;/td&gt;
     &lt;td&gt;8&lt;/td&gt;
     &lt;tD&gt;120&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  </code></pre>
  
  <p>No problem so far. The hard part is to use actual data from DB and insert them to the table.</p>
  
  <p>Headers "trend a", "trend b" etc are not constants. The below tables will be displayed on different countries. Some countries have specific trend names. Let's say England has "Trend A", "Trend B" and "Trend D". Italy has only "Trend A" etc.</p>
  
  <p>Is it possible to write php code and in some way auto generate the table headers based on the query results?</p>
  
  <p>And then split the results by trend so that correct values will go to correct trend?</p>
  
  <p>I know how to do a simple table like:</p>
  
  <pre><code>$sql_r = "SELECT o.id, o.code, tl.name, ts.items, ts.revenue
            FROM overview o, trend_stats ts, trend_lang tl           
                      WHERE o.id_shop = '1'
                      AND o.id = ts.id
                      AND ts.id_trend = tl.id_trend";
  $data_to_output = Db::getInstance()-&gt;ExecuteS($sql_r);
  
  foreach ($data_to_output as $data) {
      echo '
          &lt;tr&gt;
          &lt;td&gt;'.$data['id'].'&lt;/td&gt;
          &lt;td&gt;'.$data['code'].'&lt;/td&gt;
          &lt;td&gt;'.$data['trend'].'&lt;/td&gt;
          &lt;td&gt;'.$data['items'].'&lt;/td&gt;
          &lt;td&gt;'.$data['revenue'].'&lt;/td&gt;
          &lt;/tr&gt; 
      ';    
  }
  </code></pre>
  
  <p>But I'm not sure how to split and structure them the way I want them to be. If there's any tutorial or anything online related to what I'm asking I'm happy to check it out. Thank you</p>