MarcieZoob
06-02-2005, 10:45 AM
We have created a staff area for our client where they can add their staff photos, name and phone number. The client has multiple departments so we created a Category Header in the database which should show the department name but will not update properly on the page. Click here to view the output:
http://tubbs.server303.com/contact-tubbs2.php
You'll notice that the header "Owners" is added to each row where the category should be (other options are Used Cars, News Cars, Office).
Here's the code:
//define query
$query="SELECT * FROM staff ORDER BY Category, List_no ASC";
$result = mysql_query($query) or die("Error: " . mysql_error());
$current_category = null;
while($row = mysql_fetch_array($result))
{
if ($current_category != $row['Category'])
{
//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'] . "
" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
$current_category = $row['Category'];
}
if ($current_category != $row['Category'])
{
//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'] . "
" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
$current_category = $row['Category'];
if ($current_category != $row['Category'])
{
//print category 3 heading
echo("<table width=\"450\" cellspacing=\"3\" cellpadding=\"4\">
<tr bgcolor=\"#EEEEEE\" class=\"tablecelltitle\">
<td width=\"175\" height=\"25\">New Vehicles</td>
<td width=\"200\" height=\"25\">Title</td>
<td width=\"75\" height=\"25\">Extension</td>
</tr>");
}
//print category 3 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'] . "
" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
$current_category = $row['Category'];
echo("</table>");
mysql_free_result($result);
RESULTS:
Category 1 Heading
Cat1 - Record 1
Cat1 - Record 2
Category 1 Heading
Cat2 - Record 1
Cat2 - Record 2
Category 1 Heading
Cat3 - Record 1
Cat3 - Record 2
Category 2 Heading
{no records}
Category 3 Heading
{no records}
DESIRED RESULTS:
Category 1 Heading
Cat1 - Record 1
Cat1 - Record 2
Category 2 Heading
Cat2 - Record 1
Cat2 - Record 2
Category 3 Heading
Cat3 - Record 1
Cat3 - Record 2
We think the problem is in the placement of the WHERE statement; we have tried numerous variations without success. If you can see something obvious in this code that would fix our error, we would appreciate your help. Thanks!
http://tubbs.server303.com/contact-tubbs2.php
You'll notice that the header "Owners" is added to each row where the category should be (other options are Used Cars, News Cars, Office).
Here's the code:
//define query
$query="SELECT * FROM staff ORDER BY Category, List_no ASC";
$result = mysql_query($query) or die("Error: " . mysql_error());
$current_category = null;
while($row = mysql_fetch_array($result))
{
if ($current_category != $row['Category'])
{
//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'] . "
" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
$current_category = $row['Category'];
}
if ($current_category != $row['Category'])
{
//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'] . "
" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
$current_category = $row['Category'];
if ($current_category != $row['Category'])
{
//print category 3 heading
echo("<table width=\"450\" cellspacing=\"3\" cellpadding=\"4\">
<tr bgcolor=\"#EEEEEE\" class=\"tablecelltitle\">
<td width=\"175\" height=\"25\">New Vehicles</td>
<td width=\"200\" height=\"25\">Title</td>
<td width=\"75\" height=\"25\">Extension</td>
</tr>");
}
//print category 3 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'] . "
" . $row['StaffTitle']);
echo("<td valign=\"top\" align=\"center\">");
echo($row['Extension']);
echo("</td></tr>");
$current_category = $row['Category'];
echo("</table>");
mysql_free_result($result);
RESULTS:
Category 1 Heading
Cat1 - Record 1
Cat1 - Record 2
Category 1 Heading
Cat2 - Record 1
Cat2 - Record 2
Category 1 Heading
Cat3 - Record 1
Cat3 - Record 2
Category 2 Heading
{no records}
Category 3 Heading
{no records}
DESIRED RESULTS:
Category 1 Heading
Cat1 - Record 1
Cat1 - Record 2
Category 2 Heading
Cat2 - Record 1
Cat2 - Record 2
Category 3 Heading
Cat3 - Record 1
Cat3 - Record 2
We think the problem is in the placement of the WHERE statement; we have tried numerous variations without success. If you can see something obvious in this code that would fix our error, we would appreciate your help. Thanks!