adamjblakey Posted September 1, 2009 Share Posted September 1, 2009 Hi, I have a function which updates a cart but it is not working correctly. Basically if i have e.g. 2 x Red Shoes 3 x Blue Shorts And then i change the qty of the blue shorts to 4 and pass to the update function it just completely removes the first lot of items in the list e.g. the 2 x red shorts. The data is stored in a session like so: 58:|Red Shoes|14.95,58:|Red Shoes|14.95,56:|Blue Shorts|8.95,56:Size|Blue Shorts|8.95,56:Size|Blue Shorts|8.95 Here is the code for the function: 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 != '') { $item = ''; $newcart .= ','.$item; } else { $item = ''; $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = preg_replace("/_/i", " ", preg_replace("/(\d+)_(\d+)/i", "$1.$2", $newcart)); break; } Thanks in advance, Adam Quote Link to comment https://forums.phpfreaks.com/topic/172682-problem-with-update-function/ Share on other sites More sharing options...
JonnoTheDev Posted September 1, 2009 Share Posted September 1, 2009 You need a more logical approach to your data storage. Products in the cart would be best stored as an associative array with the index being the product id. When you add/modify a product in/to the cart the initial lookup should be against its id. Add to cart 1. Check if product is in cart i. If product is in cart, increase quantity by quantity value + current quantity end 2. Add product to cart // cart array[123] = array('name' => 'red shoes', 'quantity' => 10, 'price' => 9.99); array[127] = array('name' => 'blue jeans', 'quantity' => 1, 'price' => 19.99); I would store a cart object in your session. Use member functions to modify the cart. class cart { private $cartContents; // check if a product is in the cart public function isInCart($productId) { } // add product to cart public function addToCart($productId, $quantity = 1) { } // update product quantity private function updateQty($productId, $quantity) { } // remove from cart public function removeFromCart($productId) { } // get cart total public function cartTotal() { } // empty cart public function emptyCart() { $this->cartContents = array(); } } [code] Quote Link to comment https://forums.phpfreaks.com/topic/172682-problem-with-update-function/#findComment-910231 Share on other sites More sharing options...
adamjblakey Posted September 1, 2009 Author Share Posted September 1, 2009 Hi, Thank you for your reply, this would be a better way to handle things but at the moment everything is all set-up and working fine apart from this minor detail. As this is currently on a working website my main concern is to get this problem fixed as fast as i can then i can look at improving the functionality. Do you have any ideas on why this problem would be occurring? Cheers, Adam Quote Link to comment https://forums.phpfreaks.com/topic/172682-problem-with-update-function/#findComment-910236 Share on other sites More sharing options...
JonnoTheDev Posted September 1, 2009 Share Posted September 1, 2009 Because your cart is being cleared as soon as the switch case is update. $newcart = ''; Looking at this code you would have to rebuild the cart every time an update is made. i.e. All current products must be in the post array. I cannot see how your implementation is working fine as it is fundemetally flawed logic. As I stated when you update a quantity you need to check the current quantity of the product. You must obtain its value from the cart then increase or decreas by the desired quantity, not rebuild the cart from scratch. Quote Link to comment https://forums.phpfreaks.com/topic/172682-problem-with-update-function/#findComment-910245 Share on other sites More sharing options...
adamjblakey Posted September 1, 2009 Author Share Posted September 1, 2009 I know this is cheeky but could you post up a modified version on the update case that would work please I did not design the original script and would be here all year otherwise. I would send you $20 via paypal if you would be so kind to do so. Quote Link to comment https://forums.phpfreaks.com/topic/172682-problem-with-update-function/#findComment-910293 Share on other sites More sharing options...
JonnoTheDev Posted September 1, 2009 Share Posted September 1, 2009 Sorry but I do not freelance but try posting on the freelance board and someone may help. Need to see more code really to see how the cart is put together. Quote Link to comment https://forums.phpfreaks.com/topic/172682-problem-with-update-function/#findComment-910298 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.