htorbov Posted February 21, 2012 Share Posted February 21, 2012 Hi! I use the following PHP Session Shopping Cart (it's ready to use) <? session_start(); function showCart() { $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } foreach ($contents as $id=>$qty) { $output[] = 'You have '.$qty.' items with ID: '.$id; } } else { $output[] = 'You don\'t have items in your cart'; } return join('',$output); } if(isset($_SESSION['cart'])) { $cart = $_SESSION['cart']; } else { $cart = 0; } if(isset($_GET['action'])) { $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } } $_SESSION['cart'] = $cart; echo showCart(); echo '<br/>'; echo '<a href="?action=add&id=1">Add to Cart</a>'; ?> Example of usage: === <a href="cart.php?action=add&id=1">Add to Cart</a> // Adding a new product in the cart with ID 1 <a href="cart.php?action=delete&id=1">Delete from Cart</a> // Deleting a new product in the cart with ID 1 <a href="cart.php?action=update&id=1&qty=5">Update Quantity to 5</a> // Updating the quantity of product with ID 1 === Everything is fine, but now I want to add sizes, something like: cart.php?action=add&id=1&size=XXXL. For example, the case "add" can be done like that: $cart = $_GET['id'].":".$_GET['size']; .. and then the products will be saved like 1:XXXL, but then the updating and deleting will not work .. Please help me, I'm very confused how this can be done Quote Link to comment Share on other sites More sharing options...
htorbov Posted February 21, 2012 Author Share Posted February 21, 2012 I'm starting to think that this is not possible :'( I tried to save the contents like: 1:XXXL, 2:S, 3:M for example but then I can't split it with explode() correctly.. Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 21, 2012 Share Posted February 21, 2012 I would store the contents of the cart in an array instead of a string. You can work on it a lot easier and you don't have to use explode a bunch of times. When you add something it might look like, $id = $_GET['id']; $size = $_GET['size']; $qty = $_GET['qty']; $cart[$id] = array('size' => $size, 'qty' => $qty); This way, you can add as much additional information about the item as you want with very little effort. Quote Link to comment Share on other sites More sharing options...
htorbov Posted February 21, 2012 Author Share Posted February 21, 2012 Thanks! Now when I type print_r($cart) I get: Array ( [1] => Array ( => XXXL [qty] => 5 ) ) Now how can I use the foreach function to see the contents as: ID: 1, Size: XXXL, Quantity: 5 (for example)? Quote Link to comment Share on other sites More sharing options...
htorbov Posted February 21, 2012 Author Share Posted February 21, 2012 Nevermind, I did it. So now the question is: what is the best way to save it in the database? Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 21, 2012 Share Posted February 21, 2012 You should try to split up the tables and normalize as best you can. For example, you might have a products table holding all of the product names. Then have a cart table, holding a user id, product id, and some options like size, color, quantity etc. Quote Link to comment 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.