473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

updating values in a $_SESSION array

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
2 16246
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ken | last post by:
How can I reset the initial form variables that are set with session statements when clicking on a button? I tried this but the function was not called: <?PHP function reset_form($none) {...
10
by: Joseph S. | last post by:
Hi, How do I pass a string from one call to a php block to another call in the same page but from another form? Here's my full php code: I'm using two forms in the same page. A hidden field...
3
by: mtjarrett | last post by:
i having trouble accessing the values from superglobal arrays. there are two situations but i'm pretty sure it's the same problem. here's the deal: on page1.php i have several check boxes. ...
6
by: Ian Davies | last post by:
Hi me again, sorry to be a pain. Ive been struggling with this one all day. Hope you can understand whats happening. First my script is below. Have a look and I'll explain at the bottom what it...
2
by: Aaron Reimann | last post by:
I have a lot of check boxes. This is an update of the check boxes, I want something was checked, then to do an insert (which is currently working), if something is no longer checked...delete the...
2
by: somecrazyguy | last post by:
Take the following code, one would think that there was absolutely no link between $test and $_SESSION. But if you reload the page, guess what... "After=FAILED". Why? Because for some reason,...
1
by: gogelpot | last post by:
Why is it that every time I run this code in a session that it does not add my new values to the session variable but it replaces the old value with the new. Here is the Code: <?php...
2
by: phpachu | last post by:
Hi, I hav created a group of textboxes using a loop and its names are unique and names are assigned using variable ( like <input type=text name=$name1>). Then How can i retrieve the values in...
18
by: John | last post by:
Something strange has just started to happen as I have been developing a db application on a site. Suddenly a session variable has stopped working as I pass it from 1 page to another (I am sure it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.