1

I'm working in a assigment and I want to save the 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

1
$_SESSION["foo"] = "bar";

Also make sure you call session_start() before ANY output to the document. I can't stress this enough. I'm talking even before the DOCTYPE deceleration.

Then you should be able to from anywhere afterwards do..

echo $_SESSION["foo"]; // output: bar
$_SESSION["foo"] = "new bar";
$_SESSION["new foo"] = $_SESSION["foo"];

and

$_SESSION["items"] = array("pants", "hat");
array_push($_SESSION["items"], "shirt");

etc.

Sign up to request clarification or add additional context in comments.

Comments

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.