Jump to content

for loop problem


geroido

Recommended Posts

Hi

I have an array created with values and am searching through a database using those values. The first value is 'Starter', the second is 'Main Courses' etc. When I display the results on the web page it is displaying them in the wrong place. The 'Starters' are appearing under the 'Main Courses', the 'Main Courses' are appearing under the 'Desserts' and so on. The items are in the database under the hewadings stored in the array. Everything is being retreived from the database but not printed in its correct place on the page. Can you help? I use the variable 'i' to increment the for loop so I know that on the very first loop, i = [0] which in turn corresponds to 'Starter' in the array so why are starters not appearing first on my webpage with the record from the database? The code is as follows:

 

<?

$itemtype = array("Starter", "Main Courses", "Desserts", "Beverages", "Extras", "Miscellaneous");

for ($i=0;$i<=5;$i++){

echo $itemtype[$i];

echo $i;

echo ("<BR>");

$query = "select orderdetails.Ordernum, orderdetails.MenuItemID, orderdetails.CustFName, orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.itemQuantity, orderdetails.itemcost, menuitemdetails.MenuItemName, menuitemdetails.MenuItemDesc, menuitemdetails.MenuItemType FROM orderdetails right join menuitemdetails on orderdetails.MenuItemID=menuitemdetails.MenuItemID where menuitemdetails.MenuItemID = orderdetails.MenuItemID AND orderdetails.OrderNum = '".$_SESSION['order']."' and MenuItemType = '".$itemtype[$i]."'";

$results = mysql_query($query, $link) or die("Sorry, but you have no orders for today");?>

<TABLE border=2 bgcolor=yellow BORDERCOLOR="#007FFF" cellpadding=300 width=900>

<TR><TD>Name</TD><TD>Description</TD><TD>ID number</TD><TD>Quantity/Cost</TD><TD>Item total</TD><TD>Item Type</TD></TR><TR><?

 

 

if(mysql_num_rows($results) > 0){

 

while($row = mysql_fetch_object($results)){

 

$overallcost = $row->itemQuantity * $row->itemcost;

$total = $total + $overallcost;

echo ("<tr>");

echo ("<td>");

echo $row->MenuItemName;

echo ("</td>");

echo ("<td>");

echo $row->MenuItemDesc;

echo ("</td>");

 

echo ("<td>");

echo $row->MenuItemID;

echo ("</td>");

echo ("<td>");

echo $row->itemQuantity . "@" . $row->itemcost;

echo ("</td>");

echo ("<td>");

echo $overallcost;

echo ("</TD>");

echo ("<td>");

echo $row->MenuItemType;

echo ("</td>");

echo ("</Tr>");

}

}

}

Link to comment
https://forums.phpfreaks.com/topic/117325-for-loop-problem/
Share on other sites

Hi arifsor

Thanks for th reply. The problem still persists. I've removed the <TR> but no luck. Any more ideas?

 

 

<?

$itemtype = array("Starter", "Main Courses", "Desserts", "Beverages", "Extras", "Miscellaneous");

for ($i=0;$i<=5;$i++){

echo $itemtype[$i];

echo $i;

echo ("<BR>");

$query = "select orderdetails.Ordernum, orderdetails.MenuItemID, orderdetails.CustFName, orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.itemQuantity, orderdetails.itemcost, menuitemdetails.MenuItemName, menuitemdetails.MenuItemDesc, menuitemdetails.MenuItemType FROM orderdetails right join menuitemdetails on orderdetails.MenuItemID=menuitemdetails.MenuItemID where menuitemdetails.MenuItemID = orderdetails.MenuItemID AND orderdetails.OrderNum = '".$_SESSION['order']."' and MenuItemType = '".$itemtype[$i]."'";

$results = mysql_query($query, $link) or die("Sorry, but you have no orders for today");?>

<TABLE border=2 bgcolor=yellow BORDERCOLOR="#007FFF" cellpadding=300 width=900>

<TR><TD>Name</TD><TD>Description</TD><TD>ID number</TD><TD>Quantity/Cost</TD><TD>Item total</TD><TD>Item Type</TD></TR><?

 

 

 

 

 

if(mysql_num_rows($results) > 0){

 

while($row = mysql_fetch_object($results)){

 

$overallcost = $row->itemQuantity * $row->itemcost;

$total = $total + $overallcost;

echo ("<tr>");

echo ("<td>");

echo $row->MenuItemName;

echo ("</td>");

echo ("<td>");

echo $row->MenuItemDesc;

echo ("</td>");

 

echo ("<td>");

echo $row->MenuItemID;

echo ("</td>");

echo ("<td>");

echo $row->itemQuantity . "@" . $row->itemcost;

echo ("</td>");

echo ("<td>");

echo $overallcost;

echo ("</TD>");

echo ("<td>");

echo $row->MenuItemType;

echo ("</td>");

echo ("</Tr>");

}

}

}

Link to comment
https://forums.phpfreaks.com/topic/117325-for-loop-problem/#findComment-603510
Share on other sites

Might be your query. You have already linked the tables with the inner join, no need to do it again in the where clause.

 

try like this

$query = "SELECT orderdetails.Ordernum, orderdetails.MenuItemID, orderdetails.CustFName, orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.itemQuantity, orderdetails.itemcost, menuitemdetails.MenuItemName, menuitemdetails.MenuItemDesc, menuitemdetails.MenuItemType
          FROM orderdetails
          RIGHT JOIN menuitemdetails ON orderdetails.MenuItemID=menuitemdetails.MenuItemID
          WHERE orderdetails.OrderNum = '".$_SESSION['order']."' and MenuItemType = '".$itemtype[$i]."'";

 

Ray

Link to comment
https://forums.phpfreaks.com/topic/117325-for-loop-problem/#findComment-603548
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.