Insert section headings - but only call 1 query
Posted: Mon May 23, 2005 11:23 am
For the sake of efficiency I am trying to use 1 query, but would like to then organize my data by section. There are 8 categories of employees, and I would like to print a heading at the beginning of each section.
Thus far I can only get the heading to appear before each record instead of before each category.
I've tried numerous variations to get the desired results but thus far have been unsuccessful.
Thus far I can only get the heading to appear before each record instead of before each category.
Code: Select all
//define query
$query="SELECT * FROM staff ORDER BY Category, List_no ASC";
$result = mysql_query($query) or die("Error: " . mysql_error());
while($row = mysql_fetch_array($result)){
if ($row['Category'] == 1){
//print category 1 heading
echo("<table width=\"450\" cellspacing=\"3\" cellpadding=\"4\">
<tr bgcolor=\"#EEEEEE\" class=\"tablecelltitle\">
<td width=\"175\" height=\"25\">Owners</td>
<td width=\"200\" height=\"25\">Title</td>
<td width=\"75\" height=\"25\">Extension</td>
</tr>");
//print category 1 records
echo("<tr><td width=\"85\">");
if (empty($row["StaffPhoto"])){
echo ("Photo</td>");
} else {
echo("<img src=\"staff/{$row['StaffPhoto']}\" width=\"75\" height=\"110\"></td>");
}
echo("<td valign=\"top\" align=\"left\">");
echo($row['StaffName'] . "<br>" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
}
elseif ($row['Category'] == 2){
//print category 2 heading
echo("<table width=\"450\" cellspacing=\"3\" cellpadding=\"4\">
<tr bgcolor=\"#EEEEEE\" class=\"tablecelltitle\">
<td width=\"175\" height=\"25\">Office</td>
<td width=\"200\" height=\"25\">Title</td>
<td width=\"75\" height=\"25\">Extension</td>
</tr>");
//print category 2 records
echo("<tr><td width=\"85\">");
if (empty($row["StaffPhoto"])){
echo ("Photo</td>");
} else {
echo("<img src=\"staff/{$row['StaffPhoto']}\" width=\"75\" height=\"110\"></td>");
}
echo("<td valign=\"top\" align=\"left\">");
echo($row['StaffName'] . "<br>" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
}
}
echo("</table>");
mysql_free_result($result);
?>