2011-04-30 55 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