I've been working on a webshop, and I've got most of the functionality up
and running. One problem, however, that I don't seem to be able to solve is
as follows. My shopping cart is stored in $_SESSION['cart'] array. As I need
to keep tabs on what items are ordered and their quantity I add elements
like this:
array_push($_SESSION['cart'],array($item => $quantity));
If I add three elements and then var_dump the global variable it spits out:
array(3) { [0]=> array(1) { ["Boxers"]=> int(4) } [1]=> array(1) {
["T-shirt"]=> int(1) } [2]=> array(1) { ["Socks"]=> int(45) } }
These value pairs are taken out like this:
foreach($_SESSION[cart] as $item => $quantity)
{
for($i = 0; $i < 1; $i++) // yes I know about this lame loop, but it
doesn't work without it
{
while (list( $product , $currQuantity) = each($quantity))
{
showCartItems($product, $currQuantity); // Iterere gjennom alle varene
i kurven
}
}
}
Sorry about the long code, but I just wanted to explain it properly.
My problem, however, is to update the elements in the array like eg. change
the quantity of socks in the above array from 45 to 120. This has left me
completely dumbfounded. The convoluted (imho) $_SESSION array makes
manipulations like this hard for a sort of newbie like me.
Does anyone know how to do this, or alternatively an easier way of storing
the array elements?
They basically need to be stored like a std::C++ pair, array[0] item =>
quantity, array[1] item => quantity.
I've been stuck on this for ages now so any helg is much appreciated.
Cheers,
Håvard