I'm a newbie at PHP and I've just run into Multidimensional arrays. I'm trying to make a "temporary shopping cart" with the help of sessions. Here's what i got so far.
Code: Select all
session_start();
//This function is called by an external form.
function addToCart($productID, $quantity) {
//if the array already exists, add up the excisting "quantity" with the new quantity
if(isset($_SESSION['product'][$productID])) {
$_SESSION['product'][$productID][$quantity] = $quantity +
$_SESSION['product'][$productID][$quantity;
}
else {
$_SESSION['product'][$productID][$quantity] = $quantity;
}
}
function viewCart() {
foreach ($_SESSION['product'] as $productID => quantity) {
print "ID: $productID Quantity:";
print $_SESSION['product'][$productID][quantity];
print "<br />\n";
}
}However this is what happens
I add the number 1... and it adds on like i want to: 1 2 3 and so on.
I add the number 2, it appears as if it starts all over: 2, 4, 6.
But then if i add another 1, it goes back to the first and continues with 4, 5, 6.
What i want however is that the numbers add up together, within each of the different ProducID arrays.
I hope that was a somewhat accurate description of the problem. As stated earlier, I'm a real newbie at this so step by
step explanations would be very much appreciated.
Thanks in advance
/Daniel