title: How to split sql loop query into sections
body: <p>I have a database table <code>haccp</code> which holds rows of <code>criteria</code> categorised by 8 <code>sections</code>.
  I'm trying to create a query to fetch all criteria and echo it under each section. For example:</p>
  
  <p><strong>Section 1</strong></p>
  
  <ol>
  <li>criteria 1 </li>
  <li>criteria 2</li>
  </ol>
  
  <p><strong>Section 2</strong></p>
  
  <ol>
  <li>criteria 1 </li>
  <li>criteria 2</li>
  </ol>
  
  <p>I tried writing 8 separate sql queries using a <code>WHERE</code> clause and used a <code>while</code> loop to echo the results under each section but this took 30 secs to run and load the page.</p>
  
  <p><strong>PREVIOUS SQL QUERY</strong></p>
  
  <pre><code>$get_delivery_criteria = $mysqli-&gt;prepare("SELECT criteria, section, hazard FROM haccp WHERE section='Delivery' ORDER BY criteria");
  $get_delivery_criteria-&gt;execute();
  $get_delivery_criteria-&gt;bind_result($criteria_db, $section, $hazard);
  $get_delivery_criteria-&gt;store_result();
  $row_cnt = $get_delivery_criteria-&gt;num_rows();
  if($row_cnt &gt;= 1)
  {
      while ($get_delivery_criteria-&gt;fetch() )
      { 
      ?&gt;
          &lt;li&gt;&lt;a href="?criteria=&lt;? echo $criteria_db; ?&gt;"&gt;&lt;? echo $criteria_db. " ". $hazard; ?&gt;&lt;/a&gt;&lt;/li&gt;    
      &lt;? 
      }
      $get_delivery_criteria-&gt;close();
  }
  </code></pre>
  
  <p>So i'm working on a new query that lists the sections in an <code>array</code> then uses <code>foreach</code> to loop through the criteria and display each under the section heading.</p>
  
  <p><strong>NEW SQL QUERY</strong></p>
  
  <pre><code>$sections = array("1.Delivery", "2.Storage", "3.Preparation", "4.Cooking", "5.Serving", "6.Cleaning", "7.Building", "8.Management");
  $get_criteria = $mysqli-&gt;prepare("SELECT criteria, section, hazard FROM haccp ORDER BY criteria");
  $get_criteria-&gt;execute();
  $get_criteria-&gt;bind_result($criteria_db, $section_db, $hazard_db);
  $get_criteria-&gt;store_result();                              
  $row_cnt = $get_criteria-&gt;num_rows();
  
  if($row_cnt &gt;= 1)
  {
      while ($get_criteria-&gt;fetch() )
      { 
          foreach ($sections as $section)
          ?&gt;
              &lt;p class="haccp-section"&gt;&lt;strong&gt;&lt;? echo $section; ?&gt;&lt;/strong&gt;&lt;/p&gt;
              &lt;div class="divider"&gt;&lt;/div&gt;
              &lt;li&gt;&lt;a href="?criteria=&lt;? echo $criteria_db; ?&gt;"&gt;&lt;? echo $criteria_db. " ". $hazard_db; ?&gt;&lt;/a&gt;&lt;/li&gt; 
          &lt;? 
      }
  }
  $get_criteria-&gt;close();
  </code></pre>
  
  <p>However, this is echo'ing the section heading for each criteria, rather than the heading the the criteria list below.</p>
  
  <p>Hope that makes sense and thanks in advance!</p>