【发布时间】:2011-04-30 00:04:29
【问题描述】:
此数据来自 POST 表单,其想法是在添加新产品时简单地添加更多行。
当前输出为:
l. Banana 3 units: 150
请查看脚本(特别是 foreach 循环):
<?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);
?>
【问题讨论】:
-
会话在您 POST 时保存,但您似乎一直在用新的 POST 数据覆盖它。例如on // 保存东西部分
-
输出应该是什么?这是一个问题网站,有助于将您的帖子形成简洁的问题。
标签: php arrays list session foreach