Creating session array vriables

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
snoop2
Forum Newbie
Posts: 2
Joined: Fri Oct 10, 2003 4:44 pm

Creating session array vriables

Post by snoop2 »

Hello,
I'm new here and also new in PHP.
I'm using session variables to create a simple shopping cart.
I would like the variables to contain product name, product quantity and product prize.

currently I only know how to create a single variable, i do it like this:

Code: Select all

if (isset($_GETї'product'])) {
if (!empty($_SESSIONї'product'])) {
$products = array_unique(array_merge(unserialize($_SESSIONї'product']),$_GETї'product']));
$_SESSIONї'product'] = serialize($products);
}
}
How do I create a single variable which is actually an array variable?


Thanks in advance.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Not too sure what you mean by "single variable which is actually an array variable"...... a single variable is a single variable and an array is an array.

The way I use arrays in sessions is simply like this......

Code: Select all

<?php
// set-up the session if it hasn't been done.
if(!isset($_SESSION["items"]))
{
     $_SESSION["items"] = array();
}

// add items to the 'items' array
$_SESSION["items"][0] = "CD";
$_SESSION["items"][1] = "Blue Parrot";

// or you can build up multi-dimensional arrays
$_SESSION["items"]["CDs"][0] = "Funky";
$_SESSION["items"]["CDs"][1] = "Jazz";
$_SESSION["items"]["Pets"][0] = "Blue Parrot";
$_SESSION["items"]["Pets"][1] = "Red Parrot";

// or make 'em even deeper........ it's up to you what you do with them
$_SESSION["items"]["CDs"]["Jazz"][0] = "Bobby Jazz";
$_SESSION["items"]["CDs"]["Jazz"][1] = "Danny Jazz";
$_SESSION["items"]["CDs"]["Funk"][0] = "Funky Bob";
$_SESSION["items"]["CDs"]["Funk"][1] = "Funky Danny";
?>
........erm, yeah. Hope that helps!
snoop2
Forum Newbie
Posts: 2
Joined: Fri Oct 10, 2003 4:44 pm

Post by snoop2 »

thank you very much this does help.
Post Reply