PHP:没有存储SESSION数据

时间:2011-04-30 00:04:29

标签: php arrays list session foreach

这些数据来自POST表单,想法只是在添加新产品时添加更多行。

目前的输出是:

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

?>

2 个答案:

答案 0 :(得分: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

因为key将是从0开始的数组索引,所以每次迭代都需要+1,否则你的列表看起来像

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

答案 1 :(得分: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 />';
    }