473,659 Members | 3,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($_SE SSION['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($_SESSI ON[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 16274
In article <3f********@new s.broadpark.no> ,
"Håvard Olerud Eriksen" <he******@PANTS yahoo.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($_SESSI ON['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
30072
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) { $_SESSION = array(); } ?> <form enctype="multipart/form-data" name="company_info" method="post" action="add_pic.php">
10
2814
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 'f' with values '1' and '2' are used to distinguish calls from one form from those from the
3
1558
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. before listing them i have the code $info = array(); and then each checkbox has
6
4628
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 does so far and is failing to do ******************************* <?php session_start(); header("Cache-control: private"); //IE 6 Fix include("myconn.php");
2
2317
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 checkbox join/link that is in the database. So, the insert/checked is working, but the "unchecking" is not working. I don't know how to compare an array of what was not checked. Here is my code: if (is_array($_POST)) {
2
1814
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, writing to $test affects the $_SESSION variable $_SESSION. session_start(); ini_set("session.gc_maxlifetime","3600"); if ($_REQUEST == "1") session_unset(); var_dump($_SESSION); if (!$_SESSION) $_SESSION = "Test"; echo...
1
1767
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 session_start(); ############## # Link to DB # ############## require_once('DB_connect.php');
2
5910
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 that textboxes after a button click with in a loop. Really I cant move from here. Can anyone plz help me.. Part of the my code is given below <html> <table> <?php // i hav an array "assocArray" and its count is "countarray" for($i = 1; $i <...
18
1733
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 was OK a few days ago). I have done some tests. $temp = 67; echo $temp; gives 67 on this page S_SESSION = $temp;
0
8337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.