现在我可以输出目前的产品,但每次的形式增加了它被重写另一个项目的时间。我想使列表增量如下:
1. Banana 3 Units, Price 350 CRC
2. Yougurt 4 Units Price 2000 CRC
3. etc etc
4. etc
当前输出仅显示最后添加的项目。
这是脚本:
<?php
session_start();
//Getting the list
$list= $_SESSION['list'];
//stock
$products = array(
'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150,
'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,
'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,
'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,
);
//Saving the stuff
$_SESSION['list'] = array(
'item' => ($_POST['product']),
'quantity' => ($_POST['quantity']),
'code' => ($_POST['code']),
);
//price
$price = $products[($_SESSION['list']['item'])] * $_SESSION['list']['quantity'];
$_SESSION['list']['price'] = $price;
//listing
echo "<b>SHOPPIGN LIST</b></br>";
foreach($_SESSION as $key => $item)
{
echo $key[''], '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price'];
}
//Recycling list
$_SESSION['list'] = $list;
echo "</br> <a href='index.html'>Return to index</a> </br>";
//Printing session
var_dump($_SESSION);
?>
由于光电离, 但现在我得到一个奇怪的输出(Y似乎是从酸奶): 项目。 Y Y unitsYquantity。 4 4 units4code。 d d unitsdprice。单元 – 2011-04-29 22:21:32
被photoioized,加布里埃尔仍然会尝试迭代foreach()中不存在的$ item []数组。 – iandouglas 2011-04-29 22:28:30
我想也许,加布里埃尔使用光化变化,但也改变你的foreach()只遍历$ _SESSION而不是$ _SESSION ['列表'] – iandouglas 2011-04-29 22:29:09