Trouble Displaying Array

I’m working on a restaurants website. I’ve set it up where a customer can come online and order items off their menu. I have then tried to create an administrator side where they can view these orders. Here is where the problem is. The display currently looks like this:

Order ID|Contact Info |Order|Specials/Instructions|Date/Time to Fill
41|Test User, [email protected], 9125555555|Fried Fish Submarine with fries|Caesar Salad|Aug 31,2007 11:45

Looks great, however, the customer ordered more than just a fish submarine. The tables are set up as follows:

Orders
ID
Cus_Name
Cus_Phone
Cus_Email
Order_Date
Specials

Order_Items
ID
Order_ID
Item_ID
Quantity

Items
ID
Item_Name
Item_Price

Here is the code I am using to display the items:

[code]$resource = myQuery(“select * from “.ORDERSTABLE.” where active=‘y’ and for_date <= NOW() order by for_date”);
$nowOrders = array();
while (($row = mysql_fetch_assoc($resource)) !== false) {
$nowOrders[] = $row;
}

$itemid = array();
foreach ($nowOrders as $o) {
$resource = myQuery(“select * from “.OLINKTABLE.” where order_id=”.$o[‘id’]."");
while (($row = mysql_fetch_assoc($resource)) !== false) {
$itemid[$o[‘id’]] = $row;
}
}

$items = array();
foreach ($itemid as $o) {
$resource = myQuery(“select * from “.ITEMSTABLE.” where id=”.$o[‘item_id’]."");
while (($row = mysql_fetch_assoc($resource)) !== false) {
$items[$o[‘order_id’]] = $row;
}
}

<?php foreach ($nowOrders as $o) { ?>
<td><?php echo $o['id']; ?></td>
<td><?php echo $o['name'].', '.$o['email'].', '.$o['phone']; ?></td>
<td><?php echo $items[$o['id']]['name']; ?></td>
<td><?php echo $o['instructions']; ?></td>
<td><?php echo $date = date("M d,Y g:i", strtotime($o['for_date']));; ?></td>
<td><input type="checkbox" name="inactive[]" value="<?php echo $o['id']; ?>"/></td>
<?php } ?>[/code]

Can anybody see what I am doing wrong? I can figure out why it is only displaying one order (even though in the table on the database side it shows multiple items under the Order_ID). I feel like it is something small as I am very close. I hope somebody can help with this.

u need multible foreach.

  1. insid the loop u are overwriting $itemid[$o[‘id’]] everytime with $row. at the end $itemid[$o[‘id’]] will only contain the last row.
    use:
    [php]
    $itemid = array();
    foreach ($nowOrders as $o) {
    $resource = myQuery(“select * from “.OLINKTABLE.” where order_id=”.$o[‘id’]."");
    while (($row = mysql_fetch_assoc($resource)) !== false) {
    $itemid[$o[‘id’]][] = $row;
    }
    }

$items = array();
foreach ($itemid as $o) {
$resource = myQuery(“select * from “.ITEMSTABLE.” where id=”.$o[‘item_id’]."");
while (($row = mysql_fetch_assoc($resource)) !== false) {
$items[$o[‘order_id’]][] = $row;
}
}[/php]

or better just on loop:
[php]$items = array();
foreach ($itemid as $o) {
$resource = myQuery(“select * from “.ITEMSTABLE.”, “.OLINKTABLE.” where “.ITEMSTABLE.”.id=item_id and order_id=”.$o[‘id’]."");
while (($row = mysql_fetch_assoc($resource)) !== false) {
$items[$o[‘order_id’]][] = $row;
}
}[/php]

  1. to fech it again u need a items-foreach-loop inside the orders-foreach-loop

[code]foreach ($nowOrders as $o) {
?>

<?php echo $o['id']; ?>
<?php echo $o['name'].', '.$o['email'].', '.$o['phone']; ?> <?php foreach($items[$o['id']] as $i) { echo $i['name'].'
'; } ?> <?php echo $o['instructions']; ?> <?php echo $date = date("M d,Y g:i", strtotime($o['for_date']));; ?> <?php } [/code]

hope this helps

P.S. i wonder why u are using $items[$o[‘id’]][‘name’] / $i[‘name’], as u said that the field is called Item_Name?
shouldent it be $items[$o[‘id’]][‘item_name’] / $i[‘item_name’] ?

Now it isn’t displaying anything at all. The page just loads empty. This appears trickier than I originally thought…

what have u done? could u post the changed script?

u should always put a error_reporting(E_ALL) at the begining of ur php-code, while developing.

Sure, I simply added the code you gave me. I fiddled with it a little and now it is giving me this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

[code]$resource = myQuery(“select * from “.ORDERSTABLE.” where active=‘y’ and for_date <= NOW() order by for_date”);
$nowOrders = array();
while (($row = mysql_fetch_assoc($resource)) !== false) {
$nowOrders[] = $row;
}

$itemid = array();
foreach ($nowOrders as $o) {
$resource = myQuery(“select * from “.OLINKTABLE.” where order_id=”.$o[‘id’]."");
while (($row = mysql_fetch_assoc($resource)) !== false) {
$itemid[$o[‘id’]][] = $row;
}
}

$items = array();
foreach ($itemid as $o) {
$resource = myQuery(“select * from “.ITEMSTABLE.” where id=”.$o[‘item_id’]."");
while (($row = mysql_fetch_assoc($resource)) !== false) {
$items[$o[‘order_id’]][] = $row;
}
}

<?php foreach ($nowOrders as $o) { ?>
<td><?php echo $o['id']; ?></td>
<td><?php echo $o['name'].', '.$o['email'].', '.$o['phone']; ?></td>
<?php foreach($items[$o['id']] as $i) { echo $i['name'].'
'; } ?> <?php echo $o['instructions']; ?> <?php echo $date = date("M d,Y g:i", strtotime($o['for_date']));; ?> <?php } ?>[/code]

my fault. the second loop trys to use the array build in the first loop. that meand its not posible to use two loops anymore. cause $o[‘item_id’] will be empty now.

so u have to use:
[php]$items = array();
foreach ($nowOrder as $o) {
$resource = myQuery(“select * from “.ITEMSTABLE.”, “.OLINKTABLE.” where “.ITEMSTABLE.”.id=item_id and order_id=”.$o[‘id’]."");
while (($row = mysql_fetch_assoc($resource)) !== false) {
$items[$o[‘id’]][] = $row;
}
}[/php]

Works perfectly now! Thank you so much! Loops make me want to scream!

Sponsor our Newsletter | Privacy Policy | Terms of Service