adi123 Posted September 22, 2010 Share Posted September 22, 2010 I have 3 actions below which control my cart. My problem is with the minus action. It's supposed to remove 1 quantity from the cart but instead it takes the quantity all the way down to 1. for example: you add 3 quantities of a usb stick and you want to remove 1 quantity, it would take the amount of quantity to 1 instead of 2. here are the functions: switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; header("Location: cart.php"); } else { $cart = $_GET['id']; header("Location: cart.php"); } break; case 'minus': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $item - $id; } 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; } can some help with the minus function. thanks Quote Link to comment Share on other sites More sharing options...
schilly Posted September 22, 2010 Share Posted September 22, 2010 What does $cart look like? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted September 22, 2010 Share Posted September 22, 2010 why dont you just store an array of all of the values in the cart? This way you can easily add/remove the items then when you want to display the items to the user you could just simply do: $cart_text = implode(',',$cart); Quote Link to comment Share on other sites More sharing options...
ignace Posted September 22, 2010 Share Posted September 22, 2010 $cart = $item - $id; should be: $cart = $newcart; Quote Link to comment Share on other sites More sharing options...
adi123 Posted September 30, 2010 Author Share Posted September 30, 2010 That doesn't work either. All it does is send the quantity back to 1 rather than remove a quantity. 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.