2011-04-29 117 views
0

编辑PHP:的foreach回声不显示任何

现在我可以输出目前的产品,但每次的形式增加了它被重写另一个项目的时间。我想使列表增量如下:

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

?> 

回答

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

?> 
+0

由于光电离, 但现在我得到一个奇怪的输出(Y似乎是从酸奶): 项目。 Y Y unitsYquantity。 4 4 units4code。 d d unitsdprice。单元 – 2011-04-29 22:21:32

+0

被photoioized,加布里埃尔仍然会尝试迭代foreach()中不存在的$ item []数组。 – iandouglas 2011-04-29 22:28:30

+0

我想也许,加布里埃尔使用光化变化,但也改变你的foreach()只遍历$ _SESSION而不是$ _SESSION ['列表'] – iandouglas 2011-04-29 22:29:09

0

你只用计算出的价格覆盖你的$ _SESSION [“名单”]的值,所以当你迭代$ _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