Session associative array help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
samp
Forum Newbie
Posts: 1
Joined: Mon Jan 12, 2004 1:37 pm

Session associative array help

Post by samp »

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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

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";

?>
Post Reply