473,395 Members | 2,446 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,395 software developers and data experts.

Array. setting item id for session based cart?

Hello,

I'm hoping someone can help me out here.
I'm building a shopping cart using sessions, populating with $_POST
values from a form. Code abbreviated below:

check if form is submitted. if so, set up variables:

$name = $_POST['name'];
$ribbon = $_POST['ribbon'];
$quantity = 1;
if (isset($_SESSION['cart'][$item_id])) $item_id = ($item_id+1);
else $item_id = 1;

now, add those values to cart array:

$_SESSION['cart'][]= array( 'name' =$name, 'ribbon' =$ribbon,
'quantity' =$quantity, 'item_id' =$item_id);
$cart = $_SESSION['cart'];

This is working mostly so far. Problem is, assigning an 'item id' to
each item in the array is failing. I'll need this later to allow
users to update quantities,delete, etc. from the cart. I'm thinking
that I'd do well to assign the array numbers (0 and 1 as shown in cart
array structure below) as an 'item id' in each item's array.
Unfortunately, I can't figure out how to do this. You'll see below
that each array's 'item_id" is getting stuck at 1. Anyone out there
have any suggestions? Thanks so much in advance, Chris.

Array
(
[0] =Array
(
[name] =Bob
[ribbon] =green multi
[quantity] =1
[item_id] =1
)

[1] =Array
(
[name] =Sally
[ribbon] =blue multi
[quantity] =1
[item_id] =1
)

)
Oct 16 '08 #1
4 6224
chrism wrote:
Hello,

I'm hoping someone can help me out here.
I'm building a shopping cart using sessions, populating with $_POST
values from a form. Code abbreviated below:

check if form is submitted. if so, set up variables:

$name = $_POST['name'];
$ribbon = $_POST['ribbon'];
$quantity = 1;
if (isset($_SESSION['cart'][$item_id])) $item_id = ($item_id+1);
else $item_id = 1;
[snip]
>
This is working mostly so far. Problem is, assigning an 'item id' to
each item in the array is failing. I'll need this later to allow
users to update quantities,delete, etc. from the cart. I'm thinking
that I'd do well to assign the array numbers (0 and 1 as shown in cart
array structure below) as an 'item id' in each item's array.
Unfortunately, I can't figure out how to do this. You'll see below
that each array's 'item_id" is getting stuck at 1. Anyone out there
have any suggestions? Thanks so much in advance, Chris.
Is $item_id defined elsewhere in your code? From the snippet above, it
looks as if you're using an undefined variable (try testing with
error_level = E_ALL in your development environment), which would
result in your if-statement always failing.

--
Curtis
Oct 16 '08 #2
Thanks Curtis. Yes, I defined $item_id earlier on. What I'm really
trying to figure out is how to set the array value $item_id equal to
the array identifier (sorry for the lack of proper terminology) as
show below. (I'd like the first array's item_id to be set to 0, and
the second array's item_id to be set to 1. this way I'll have
something to refer to the arrays by later when i need to update the
cart.)

Array
(
[0] =Array
(
[name] =Bob
[ribbon] =green multi
[quantity] =1
[item_id] =1
)

[1] =Array
(
[name] =Sally
[ribbon] =blue multi
[quantity] =1
[item_id] =1
)

)
Oct 16 '08 #3
Message-ID:
<0e**********************************@u28g2000hsc. googlegroups.comfrom
chrism contained the following:
>Thanks Curtis. Yes, I defined $item_id earlier on. What I'm really
trying to figure out is how to set the array value $item_id equal to
the array identifier (sorry for the lack of proper terminology) as
show below. (I'd like the first array's item_id to be set to 0, and
the second array's item_id to be set to 1. this way I'll have
something to refer to the arrays by later when i need to update the
cart.)

You don't need to. The session array key (that's the word you are
searching for) will increment automatically. You don't need an item id
at all. In fact, a lot of your code is redundant.

All you need is.

$_SESSION['cart'][]= array( 'name' =$_POST['name'], 'ribbon' =>
$_POST['ribbon'],'quantity' =1);

Although you could have 'quantity' =$_POST['quantity'] if you have
the appropriate form element.

When you loop through the array of cart items to display the contents:

foreach($_SESSION['cart'] as $key=>$value){
//Loop the array, $value, to show the item details. $key will contain
//the key identifying the item. Pass it back by adding it as a query
//string to a url e.g.
//<a href='edit cart_items.php?item=$key'>edit cart item</a>
}

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
Oct 16 '08 #4
This is great. Thanks a lot, Geoff.

Oct 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Josef Blösl | last post by:
hi folks! i have a cart link in all of my listed products on the site. when i press this link i will generate a url like this ...
4
by: yohomonkey | last post by:
I want to setting the session end , but ican't waiting for the session is timeout! Who can help me?
0
by: Roger Stavcode | last post by:
Is it possible to point current context's session to another active session based on a SessionID? Thank you STAVCOQ
3
by: Brad | last post by:
I am storing an array which contains about a dozen chracter items to a Session variable. Later, I need to use this array so I am doing the following: Dim eventTypes As String() =...
4
by: MrBiggles | last post by:
Here's the sitch: I read in a csv file with 60000 lines (20 fields per record), and store the data to a local array. The file is read in and stored just fine and pretty quick. Now, if I try to...
4
by: awebguynow | last post by:
My Auth code works locally but not on my host, and I'm debugging it to find out, why not. Except where noted, I'll be referring to my webhost's configuration. I'm using a .htaccess "AddHandler"...
43
by: davidkoree | last post by:
I mean not about cookie. Does it have something to do with operating system or browser plugin? I appreciate any help.
5
by: kumuda v | last post by:
Hello all, I want to pass the array from one php page to other php page. So how to make array as an session variable. with regards
5
by: rahia307 | last post by:
i developed session based shopping cart. using this code. $_SESSION= array( 'prd_name' => $prd_name, 'unit_price' => $unit_price, 'qty' => $qty, 'id_prd' => $id_prd); and i use this code to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.