PHP:未存储SESSION数据



这个数据来自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);
?>

更改此代码:

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

现在,每次POST到页面时,您都会在$_SESSION中获得一个新条目。

同样,如果您希望您的输出看起来像:

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

则需要更改foreach()循环中的echo

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

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

由于key将是从0开始的数组索引,因此您需要在每次迭代中执行+1,否则您的列表将看起来像

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

正如ianddouglas所写,您每次都覆盖会话变量。以下代码还修复了一些未定义的索引问题和已经存在的产品。

    // 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 />';
    }

最新更新