...

View Full Version : Adding headers within loops



gmnightingale
05-28-2010, 10:23 PM
I am currently trying to construct a menu system. You can view this at

www.actioncomputing.co.uk/test/index.php please choose the thai option as that has entrys.

I need to have a header for each mealType but no matter what I do I cant get it to appear ONLY before the first entry of a meal type. such as

STARTERS

Crispy Mini Spring Rolls Very tasty crispy Mini Spring Rolls 1 Thai Starter 4.60

--------------------------------------------------------------------------------

a starter a starter a starter a starter 1 Thai Starter 3.60

--------------------------------------------------------------------------------

Chicken Satay Finest Chicken Satay 1 Thai Starter 5.70

--------------------------------------------------------------------------------
MAINS

b main b main b main b main 2 Thai Main 2.60

--------------------------------------------------------------------------------
Desserts

Thai green chicken Very Nice Thai green chicken 3 Thai Dessert 7.40


I have added the key and the mealType at the end of the echo just to help see whats going on.

this is my code
[CODE]

<?php

$passedMenuType = (@$_GET['menuType']);
$query = "SELECT * FROM meal LEFT JOIN mealType ON meal.mealType=mealType.mealType Order by mealTypeOrder";

$result5 = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");

echo "<h1>$passedMenuType menu</h1>";

echo "<table cellpadding='5' border='0' cellspacing='15' width='580px'> <tr> <td colspan='3'><hr /></td></tr>";

while($row = mysqli_fetch_assoc($result5))
{
extract($row);
$f_mealPrice = number_format($mealPrice,2);

if($menuType == $passedMenuType) {


echo "<tr>";
echo "
<td>$mealName</td>
<td>$mealDescription</td>
<td>$mealTypeOrder</td>
<td>$menuType</td>
<td>$mealType</td>
<td>$f_mealPrice</td></tr>" ;
echo"<tr> <td colspan='3'><hr /></td></tr>";

}
}
echo"</table>"; ?>
[ICODE]

Can anyone suggest how I can create these headers within this loop?

TheShaner
05-28-2010, 10:40 PM
You can always use a variable that stores the last meal type used. Then you just check to see if the meal type from the DB is the same as the last meal type variable. If it isn't, print the header; otherwise, don't print it. This of course relies on the fact that all the meal types are grouped together, meaning you've ORDERed on meal type.

For example:


if($mealTypeOrder != $lastMealTypeOrder)
{
echo "<h2>" . $mealType . "</h2>";
$lastMealTypeOrder = $mealTypeOrder;
}
Note: Be sure you initialize the $mealTypeOrder variable before the loop and with a value you wouldn't find in
$mealTypeOrder, yet is of the same type. This means if $mealTypeOrder holds integers, initialize it with an integer that you would never get in the DB, i.e. -1 if all mealTypeOrders are 0 - 5.

-Shane

gmnightingale
05-28-2010, 10:54 PM
You can always use a variable that stores the last meal type used. Then you just check to see if the meal type from the DB is the same as the last meal type variable. If it isn't, print the header; otherwise, don't print it. This of course relies on the fact that all the meal types are grouped together, meaning you've ORDERed on meal type.

For example:


if($mealTypeOrder != $lastMealTypeOrder)
{
echo "<h2>" . $mealType . "</h2>";
$lastMealTypeOrder = $mealTypeOrder;
}
Note: Be sure you initialize the $mealTypeOrder variable before the loop and with a value you wouldn't find in
$mealTypeOrder, yet is of the same type. This means if $mealTypeOrder holds integers, initialize it with an integer that you would never get in the DB, i.e. -1 if all mealTypeOrders are 0 - 5.

-Shane

Hi Shaner,

I tried this but it echo all the headers before any of the meals as you can see if u re visit my site, can you implement it in my code from above please and I will check that

gmnightingale
05-29-2010, 02:06 AM
all sorted thanks for your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum