Jump to content

PHP Shopping Cart


Andy11548

Recommended Posts

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
https://forums.phpfreaks.com/topic/288321-php-shopping-cart/#findComment-1478638
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
https://forums.phpfreaks.com/topic/288321-php-shopping-cart/#findComment-1478641
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.