By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,544 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,544 IT Pros & Developers. It's quick & easy.

updating values in a $_SESSION array

P: n/a
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
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <3f********@news.broadpark.no>,
"Håvard Olerud Eriksen" <he******@PANTSyahoo.com> wrote:
Does anyone know how to do this, or alternatively an easier way of storing
the array elements?


initialize the array with:

$_SESSION['cart'] = array();

then add items like this:

$_SESSION['cart']['Boxers'] = 4;
$_SESSION['cart']['T-shirts'] = 2;
$_SESSION['cart']['Socks'] = 45;

It gives you a much simpler array structure:

print_r($_SESSION['cart']);

Array
(
[Boxers] => 4
[T-shirts] => 2
[Socks] => 45
)

Simply update the quantities with:

$_SESSION['cart']['Socks'] = $new_quantity;
$_SESSION['cart']['Socks'] += $quantity;

or similar.

HTH,
JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 16 '05 #2

P: n/a
> Array
(
[Boxers] => 4
[T-shirts] => 2
[Socks] => 45
)

Simply update the quantities with:

$_SESSION['cart']['Socks'] = $new_quantity;
$_SESSION['cart']['Socks'] += $quantity;


Thanks a lot for the quick answer. Well, this certainly make things a lot
easier. Also stuff like checking for an existing key, then updating the
quantity of that instead of making a separate array element etc. will be a
breeze.

Cheers,
Håvard
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.