2011-04-30 56 views
0

該數據來自POST形式,這個想法是簡單地添加時增加了一個新的產品更行。PHP:SESSION數據沒有被存儲

的電流輸出爲:

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

?> 
+0

會話保存爲POST,但它似乎保持覆蓋與一個新的POST數據.e.g。上//保存填充部分 – tradyblix 2011-04-30 00:09:25

+0

什麼是輸出應該是什麼?這是一個問題網站,它有助於將您的帖子形成簡明的問題。 – 2011-04-30 00:09:38

回答

0

正如iandouglas寫道,你每次覆蓋會話變量。以下代碼還修復了一些未定義的索引問題和已有的產品。

// Test POST data. 
    $_POST['product'] = 'Pineaple'; 
    $_POST['quantity'] = 1; 
    $_POST['code'] = 1; 

    //Getting the list 
    $_SESSION['list'] = isset($_SESSION['list']) ? $_SESSION['list'] : array(); 

    //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 
    $new_item = array(
     'item' => $_POST['product'], 
     'quantity' => $_POST['quantity'], 
     'code' => $_POST['code'], 
     'price' => $products[$_POST['product']] * $_POST['quantity'], 
    ); 

    $new_product = true; 
    foreach($_SESSION['list'] as $key => $item) { 
     if ($item['item'] == $new_item['item']) { 
     $_SESSION['list'][$key]['quantity'] += $new_item['quantity']; 
     $_SESSION['list'][$key]['price'] = $products[$new_item['item']] * $new_item['quantity']; 
     $new_product = false; 
     } 
    } 

    if ($new_product) { 
     $_SESSION['list'][] = $new_item;  
    } 
    //listing 
    echo "<b>SHOPPIGN LIST</b></br>"; 
    foreach($_SESSION['list'] as $key => $item) { 
     echo 'key '. $key. ' '. $item['item'], ' ', $item['quantity'], ' units: ', $item['price']. '<br />'; 
    } 
+0

非常感謝!順便說一句,你知道如何呼應警告,並刪除所有$ NEW_ITEM,是不是等於1 $產品陣列中的? – Gabriel 2011-04-30 05:05:58

+0

定義頂部您的有效產品,只是包裝內這一切: '如果(in_array($ _ POST [「產品」],$產品){ //把你的代碼在這裏 } 其他{ 回聲「無效的產品'; }' – Borgenk 2011-04-30 21:23:51

+0

這應該是'if(in_array($ _ POST ['product'],array_keys($ products))){' – Borgenk 2011-04-30 21:36:39

1

改變這樣的代碼:

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

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

,並刪除此代碼:

//Recycling list 
$_SESSION['list'] = $list; 

現在,你會每次你郵寄到頁面時,得到$ _SESSION一個新的條目。

另外,如果你希望你的輸出看起來像:

l. Banana 3 units: 150 
2. Orange 5 units: 250 
etc 

那麼你就需要從foreach()循環改變回聲

echo $key[''] . // everything else 

只是

echo ($key+1) . // everything else 

由於密鑰將您的數組索引從0開始,你需要做一個+1每次迭代,否則你的列表WOU LD看起來像

0. Banana 3 units: 150 
1. Orange 5 units: 250 
+0

嗨,戴夫,很好的幫助!實際上這個數組正在被保存,但是現在回聲消失了,並且出現了一些錯誤。 公告:未定義的(項目,數量)的行項目26個36 – Gabriel 2011-04-30 00:33:43