geroido Posted July 30, 2008 Share Posted July 30, 2008 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 More sharing options...
arifsor Posted July 30, 2008 Share Posted July 30, 2008 <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><? remove <TR> Link to comment https://forums.phpfreaks.com/topic/117325-for-loop-problem/#findComment-603487 Share on other sites More sharing options...
geroido Posted July 30, 2008 Author Share Posted July 30, 2008 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 More sharing options...
rameshfaj Posted July 30, 2008 Share Posted July 30, 2008 I think you are misplacing the headers or the desired column of the record are not accessed accordingly.What I suggest you is to try with displaying one column at a time and then trace where is the misunderstanding,this is not the problem actually. Link to comment https://forums.phpfreaks.com/topic/117325-for-loop-problem/#findComment-603536 Share on other sites More sharing options...
craygo Posted July 30, 2008 Share Posted July 30, 2008 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 More sharing options...
geroido Posted July 30, 2008 Author Share Posted July 30, 2008 Thanks all It was just to do with the positioning within the loop. Always gets me that one. Link to comment https://forums.phpfreaks.com/topic/117325-for-loop-problem/#findComment-603577 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.