Jump to content

Online Order System Looping Problem!


mikebarbaro

Recommended Posts

Hello,

 

I am designing an online ordering system for a restaurant. I have a menu with check boxes by each item where the customer can select which items they desire. The menu is located at tandoorpgh.com/placeanorder

 

The problem is that when the order is processed I need it to store each checked off item as a session variable. In the following code, it only selects the first item checked and not the rest if there is more than one. Any help is appreciated!

 

$item=$_POST['title'];
foreach ($item as $itemname)
{

$query = mysql_query("SELECT * FROM `menu` WHERE title = '$itemname'");
while($row = mysql_fetch_array($query)) {

	$total = $total + $row['price'];

echo "<p>" . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span></p>";

$allitems = $row['title'] . $row['price'];

$_SESSION['items'] = $allitems;
$_SESSION['total'] = $total;
}
}

Link to comment
https://forums.phpfreaks.com/topic/189787-online-order-system-looping-problem/
Share on other sites

Well, my biggest confusion is the following code snippet containing the echo displays all items that were checked off from the form, yet when I try and save them as a session variable it only displays one item.

 

echo "<p>" . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span></p>";

 

Say three items from the menu at tandoorpgh.com/placeanorder are checked off. When they come to this page to review their order the echo statement displays all three, but I cannot figure out why they won't save as a session variable?

 

I've tried

$_SESSION['items'] = $row['title'];

within the loop statement but no luck...

 

Do I maybe need to loop through the whole array?

 

Any help is very much appreciated!

oh i see. well each time you loop through that while you are overwriting the session var from the previous loop. so you might want to do something like:

 

$_SESSION['items'][] = $allitems;
$_SESSION['total'] += $total;

 

that will add up the total and create an array of items.

 

ok can you post the output of this

<?PHP
echo "<pre>" . print_r($_POST,true) . "</pre>";

$item=$_POST['title'];
foreach ($item as $itemname)
{

$query = mysql_query("SELECT * FROM `menu` WHERE title = '$itemname'");
while($row = mysql_fetch_array($query)) {

	$total = $total + $row['price'];

echo "<p>" . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span></p>";

$allitems = $row['title'] . $row['price'];

$_SESSION['items'][] = $allitems;
$_SESSION['total'] += $total;
}
}

echo "<pre>" . print_r($_SESSION,true) . "</pre>";

?>

 

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.