Jump to content

PHP Shopping Cart


Andy11548
Go to solution Solved by Andy11548,

Recommended Posts

Hello,

 

I'm looking to build my own Shopping Cart (Just to have a mess around), and I'm wondering what the best way of Adding/Removing items to the cart would be?

 

Any help/assistance would be grateful.

 

 

 

Kind Regards,

Andy

Link to comment
Share on other sites

Have you a way to store/save users "cart"?

 

If not, you could save the ID of the items, in an array, which you save in the users session.

 

Then you could go through all the ID's in the array, to quickly show everything.

 

When deleting you could use the array search function, perhaps?

<?php
	session_start();

	// Item ID's in cart
	$_SESSION['cart'] = array('1', '3');

	$cart = $_SESSION['cart'];

	// Add new item to cart
	array_push($cart, '4') // This adds 4 to the end of array so cart now contains 1, 3 and 4.

	// Remove item
	if(($key = array_search('3', $cart)) !== false) {
    	unset($cart[$key]);
	} else {
		// Not found in cart
	}

	// Go through all items in cart
	if(count($cart) > 0) {
		foreach($cart as $id) {
			// Do something with the item id..
		}
	}
?>

I haven't tested the code above, so it's probably either not working, or just bad. But it can give an idea/starting point.

 

Good luck ;)

Link to comment
Share on other sites

the best method of storing a cart is to store it in a database table as this allows you to directly join the cart contents with the product information using one query when you need to display the cart information.

 

to add, subtract, or delete items from the cart you use appropriate database query statements.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.