2011-04-29 47 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