Andy11548 Posted May 7, 2014 Share Posted May 7, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288321-php-shopping-cart/ Share on other sites More sharing options...
TrickyInt Posted May 7, 2014 Share Posted May 7, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288321-php-shopping-cart/#findComment-1478638 Share on other sites More sharing options...
mac_gyver Posted May 7, 2014 Share Posted May 7, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288321-php-shopping-cart/#findComment-1478641 Share on other sites More sharing options...
Solution Andy11548 Posted May 7, 2014 Author Solution Share Posted May 7, 2014 Thanks both for the suggestions. I'll have a think, and when I get to that part, I'll choose the most appropriate. Thanks again! Andy Quote Link to comment https://forums.phpfreaks.com/topic/288321-php-shopping-cart/#findComment-1478652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.