Solved

PHP shopping carts and Key Value Pairs

Posted on 2007-03-31
1
292 Views
Last Modified: 2010-08-05
I am attempting to create a PHP shopping cart using a session. Below is the code I have so far for adding to the cart.

session_start();
$cart = $_SESSION['cart'];
$cart[sizeof($cart)+1] = $_GET['pid'] ;
$_SESSION['cart'] = $cart;

I want to store the quantity ordered as a key value pair in the array. I have tried this using the below code, without any luck. How do I do this?

$cart[sizeof($cart)+1] = ($_GET['pid'] => $_GET['quantity']);

Many Thanks
0
Comment
Question by:inghfs
1 Comment
 
LVL 24

Accepted Solution

by:
glcummins earned 500 total points
ID: 18829428
Rather than creating the intermediary variable, you can create the new key directly in the cart $_SESSION array:

$_SESSION['cart'][$_GET['pid']] = $_GET['quantity'];
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now