0

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($_SESSION);

?>
2
  • session is saved as you POST but it seems you keep overwriting it with a new POST data .e.g. on // saving the stuff part Commented Apr 30, 2011 at 0:09
  • what's the output supposed to be? This is a question site and it would help to form your posts into concise questions. Commented Apr 30, 2011 at 0:09

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi dave, great help! The array is being saved actually, but now the echo is gone, and there some errors popping up. Notice: Undefined (item, quantity) item in line 26 and 36.
0

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

3 Comments

Thanks a lot! By the way do you know how to echo a warning and remove every $new_item that is not equal to one in the $products array?
Define your valid products on top and just wrap everything else inside this: if (in_array($_POST['product'], $products) { // place your code here } else { echo 'Invalid product'; }
That should have been if (in_array($_POST['product'], array_keys($products))) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.