I'm writing a shopping cart from scratch. To save my variables from script to script, I've decided to define a session array. Here is the defintion:
$_SESSION["product"] = array(
"model" => $model,
"qty" => $qty,
"price" => $price,
"color" => $color1,
"image" => $image
);
I'm passing the variables from a previous script that shows the catalog. I want to be able to update the array dynamically each time a new product is added to the shopping cart, but I'm having trouble with the code. I guess I'm using this array as kind of a record and I want to be able to address each 'record' and print it out.
I know I can also write a user file to the MySQL database and I will do that if I can't get my code resolved. I don't want to use cookies since a lot of people have cookies turned off.
Or should I just use a multi-dimensional array?
Thanks for any help you can give me.
Session associative array help
Moderator: General Moderators
A multi-dimensional array would be easier to use, you could then use objects to make things easier to read...
Code: Select all
<?php
$_SESSION["cart"][0]->product = "Soap";
$_SESSION["cart"][0]->price = "3.99";
$_SESSION["cart"][0]->colour = "blue";
$_SESSION["cart"][1]->product = "Car";
$_SESSION["cart"][1]->price = "25,000";
$_SESSION["cart"][1]->colour = "black";
?>