开发者

PHP: SESSION data not being stored

This data comes from a POST form, the idea is to simply add more lines whenever a new product is added.

The current output is:

l. Banana 3 units: 150

Please take a look into the script (specially the foreach loop):

    <?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($_SE开发者_如何转开发SSION);

?>


change this code:

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

to

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

and remove this code:

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

Now, you'll get a new entry in $_SESSION every time you POST to the page.

Also, if you want your output to look like:

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

then you'll need to change the echo in the foreach() loop from

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

to just

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

Since key will be your array index starting at 0, you'll need to do a +1 every iteration, otherwise your list would look like

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


As iandouglas wrote, you are overwriting the session variable every time. The following code also fixes some undefined index issues and already existing products.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜