Page 1 of 1

Creating session array vriables

Posted: Fri Oct 10, 2003 4:44 pm
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.

Posted: Fri Oct 10, 2003 5:25 pm
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!

Posted: Fri Oct 10, 2003 5:28 pm
by snoop2
thank you very much this does help.