Solved

PHP shopping carts and Key Value Pairs

Posted on 2007-03-31
1
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

627 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