【问题标题】:PHP: SESSION data not being storedPHP:未存储会话数据
【发布时间】:2011-04-30 00:04:29
【问题描述】:

此数据来自 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);

?>

【问题讨论】:

  • 会话在您 POST 时保存,但您似乎一直在用新的 POST 数据覆盖它。例如on // 保存东西部分
  • 输出应该是什么?这是一个问题网站,有助于将您的帖子形成简洁的问题。

标签: php arrays list session foreach


【解决方案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

【讨论】:

  • 嗨,戴夫,帮了大忙!数组实际上正在保存,但是现在回显消失了,并且弹出了一些错误。注意:第 26 行和第 36 行中未定义(项目、数量)项目。
【解决方案2】:

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

【讨论】:

  • 非常感谢!顺便说一句,您知道如何回显警告并删除 $products 数组中不等于 1 的每个 $new_item 吗?
  • 在顶部定义您的有效产品,然后将其他所有内容包装在其中:if (in_array($_POST['product'], $products) { // place your code here } else { echo 'Invalid product'; }
  • 应该是if (in_array($_POST['product'], array_keys($products))) {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 2013-06-10
相关资源
最近更新 更多