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