Solved

PHP shopping carts and Key Value Pairs

Posted on 2007-03-31
1
297 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to dynamically set the form action using jQuery.

808 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