Hey guys i'm pack for more help again!

For a site I'm making I need a shopping cart. It's all working but for some added functionality I would like to be able to update quantity of an item using an input box. For an example see this site: http://www.oonaghnaturals.com/ShoppingCart.php?

I'm using sessions to work my cart. Below is how i access my items in the shopping cart

foreach ($_SESSION['cart'] as $product_id => $quantity){

I hope i'm being clear? If not apologies and I can clear anything up.

Cheers guys

Recommended Answers

All 5 Replies

maybe Javascript would be better if it is to be updated by the user before submission?

Well I'm going to have them submit their change.

On the shopping cart page it will say

item = shoe
qty = 1 (where the 1 is will be an input box)

(update cart button)

So I would be having them submit it.

I understand WHAT you are trying to do, but not the methodology.
Unless you are saving the data to a db, why bother burdening the server for a quantity change? If you are saving to a db, why not just fetch 'n display?
I don't see why you would want to store the data in a PHP static global..
Maybe I just don't understand your application.

Sorry if I've confused the issue. My heads all over the place at the moment.

Edit: I'm not using a db or global variables. I'm using sessions to store the information (sessions actually maybe global variables I just dont know?)

When a user decides on a product and they hit add to cart, I send them to the cart.php page using

cart.php?action=add&id=1

On my cart.php page I get the action add. which does this:

switch($action) {    //decide what to do    
		
		case "add":
			if (isset ($_SESSION['cart'][$product_id]))
				$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
			else
				$_SESSION['cart'][$product_id]=1;//if there isn't a product already then put it in
		break;

Using the following I then get then extract the quantity of each item:

foreach ($_SESSION['cart'] as $product_id => $quantity)

Below this then is just the code to display the cart

Each time I add a product to the cart it performs the above, Incrementing the product quantity where necessary.

So currently if I want to get 10 of product 1 I have to add it to the cart 10 times. I just need to come up with a way to be able to change the quantity by user inputting a number

Sorry if I've confused the issue. My heads all over the place at the moment.

Edit: I'm not using a db or global variables. I'm using sessions to store the information (sessions actually maybe global variables I just dont know?)

When a user decides on a product and they hit add to cart, I send them to the cart.php page using

cart.php?action=add&id=1

On my cart.php page I get the action add. which does this:

switch($action) {    //decide what to do    
		
		case "add":
			if (isset ($_SESSION['cart'][$product_id]))
				$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
			else
				$_SESSION['cart'][$product_id]=1;//if there isn't a product already then put it in
		break;

Using the following I then get then extract the quantity of each item:

foreach ($_SESSION['cart'] as $product_id => $quantity)

Below this then is just the code to display the cart

Each time I add a product to the cart it performs the above, Incrementing the product quantity where necessary.

So currently if I want to get 10 of product 1 I have to add it to the cart 10 times. I just need to come up with a way to be able to change the quantity by user inputting a number

Yikes! There will be smoke coming from the server if business gets real busy! LOL
Just put an input tag there for the user to put a number into.
But really this is a much better thing to do with Javascript. You can do all kinds of things either a qauntity drop down list or a number or even an up/down arrow to increase or decrease the quantity. All of this, without hitting the server.
When the user is all happy with his entries...THEN do the submit processing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.