現在我可以輸出目前的產品,但每次的形式增加了它被重寫另一個項目的時間。我想使列表增量如下:
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