0

I want to save this data into a SESSION so it can be modified by the user(as a primitive shopping cart), but I need some light in here.

A) The information comes from a POST form.

B) The output should look like this:

SHOPING LIST
1. Coffe 5 units, 6 USD.
2. Banana 3 units, 3 USD.
3. Etc (The list can be infinite)

C) This is my current code, as you can see there is no session. And I need the user to be able to add more items.

 <?php

//Variables
$item= $_POST['item'];
$quantity= $_POST['quantity'];
$code= $_POST['code'];


//List
$articulos = array(

  'Pinaple' => 1, 'Banana' => 2, 'Aple' => 3, 
  'Milk' => 1, 'Coffe' => 3, 'Butter' => 1,
  'Bread' => 2, 'Juice' => 1, 'Coconuts' => 1,
  'Yogurt' => 2, 'Beer' => 1, 'Wine' => 6,
  );

//Price
$price = $items[$item] * $quantity;

//Shoping List

echo  "<b>Shopping List</b></br>";


echo "1. ".$item." ".$quantity." units".", ".$price." USD.";

//Back to index
echo "</br> <a href='index.html'>Back to Index</a>";


?>

1 Answer 1

2

Put session_start(); at the start of your script, you can then put anything you want into $_SESSION, including arrays of products.

$_SESSION['cart'] = $whatever_variable_you_want;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help David, however I'm bit stuck, here is what I did: $_SESSION['cart'][1] = $item; $_SESSION['cart'][2] = $quantity; $_SESSION['cart'][3] = $price; I get the following error for each statement: Notice: Undefined variable:
Sorry Gabriel, the entire error didn't come through. I'm guessing the $item, $quantity and $price variables are what it's saying are undefined. Perhaps edit your post with your current script? Try this: $_SESSION['cart'][] = array('price' => $price, 'item' => $item, 'qty' => $quantity);
Thanks David, now is working! Now I'm trying to figure out how to echo those array values, I'm getting blank spaces even though var_dump is showing everything: echo "1. ".$_SESSION['cart']['1']." ".$_SESSION['cart']['2']." units".", ".$_SESSION['cart']['3']." USD.";
In the structure I gave you it would be: $_SESSION['cart'][0]['item'], etc

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.