PHP:Foreach echo没有显示任何内容

时间:2011-04-29 22:05:58

标签: arrays list session foreach shopping-cart

修改

现在我可以输出当前产品,但每次表单添加另一个项目时,它都会被覆盖。我想像这样增加列表增量:

 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);

?>

2 个答案:

答案 0 :(得分:1)

这一行是你的问题。

$_SESSION['list'] = array('price' => $price,);

您正在设置您尝试迭代的变量,使其成为一个包含单个条目的数组,更不用说$price不是嵌套数组的事实,这是为什么试图让item['key']失败(如在'price'中将是您的密钥,而$price将成为您在foreach中的项目。)

编辑:

我相信,从第二眼看,你真的打算这样做:

$_SESSION['list']['price'] = $price;
如果我错了,请纠正我。

编辑2:

实际上,再看一遍,我不太清楚我理解你的$_SESSION['list']变量的结构。看起来你想要像:

(('item' => 'Banana', 'quantity' => 1...), ('item' => 'Apple', 'quantity' => 2...))

但你所拥有的(来自你引用$_SESSION['list']['item']的事实)只是:

('item' => 'Banana', 'quantity' => 1...)

你这里确实有多个问题。首先尝试处理$_SESSION['list']的错误结构,然后尝试处理foreach循环。

编辑3:

我仍然认为你并不完全理解我的意思,所以我只是要修改代码,以确保你正在寻找......

我很确定你的目标是什么样的:

<?php

session_start();
$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,
);

if(!array_key_exists('list', $_SESSION)){
  $_SESSION['list'] = array();
}

$price = $products[$_POST['product']] * $_POST['quantity'];
array_push($_SESSION['list'],
           array(
             'item' => $_POST['product'], 
             'quantity' => $_POST['quantity'],
             'code' => $_POST['code'],
             'price' => $price,
          ));    

echo  "<b>SHOPPING LIST</b></br>";    
foreach($_SESSION['list'] as $key => $item) {
    echo $key+1, '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price'];
}

echo "</br> <a href='index.html'>Return to index</a> </br>";

?>

答案 1 :(得分:0)

你只用计算出的价格覆盖你的$ _SESSION ['list']值,所以当你遍历$ _SESSION ['list']时,$ item中唯一的东西是计算的标量值小计,而不是您希望在“保存内容”评论中使用的数组。

如果您只是打算打印出产品/数量/代码/价格数组,这应该有效:

<?php

session_start() ;

//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,
    );  

$_POST['product'] = 'Banana' ;
$_POST['quantity'] = 50 ;
$_POST['code'] = "unknown" ;

//Saving the stuff
$_SESSION['list'] = array(
    'item' => $_POST['product'],
    'quantity' => $_POST['quantity'],
    'code' => $_POST['code'],
    'price' => $products[$_POST['product']] * $_POST['quantity'],
    );  

print_r($_SESSION['list']) ;

//listing
echo  "<b>SHOPPING LIST</b></br>\n";

foreach($_SESSION as $key => $item) {
    echo $key, '. ', $item['item'], ' ', $item['quantity'], ' units ', $item['price']."\n";
}

echo "</br> <a href='index.html'>Return to index</a> </br>";

//Recycling list
$_SESSION['list'] = ''; 
//Printing session
print_r($_SESSION);

?>

这将打印此结果:

list. Banana 50 units 2500