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 Code (markup): 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; } PHP: Thanks in advance, Adam
It seems as though case 'update': if ($cart) { PHP: this line : $newcart = ''; PHP: is creating the problem as it empties the current contents of $newcart , red shoes and blue shorts you added earlier . try removing the 1st instance of $newcart = ''; PHP: I hope it helps
hmm... is it? Is this post , the case you have posted ..is it within a function ? If so , have you tried declaring $cart as global again within the function ? function updateSumthing() { global $cart; switch { //switch cases } } PHP: By declaring $cart global within the function, all references to the variable will refer to the global version of $cart which may contain the previously stored items.
It is not inside a function. This is the whole lot of code: switch ($action) { case 'add': $new = ''; $qty = $_POST['qty']; for ($i = 0; $i < $qty; $i++) { $new .= $_GET['id']; unset($_POST['qty']); unset($_POST['button']); foreach ($_POST as $post => $val) { $new .= ':' . $val; } $new .= ','; } $new = substr($new, 0, -1); if ($cart) { $cart .= ',' . $new; } else { $cart = $new; } 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 != '') { $item = ''; $newcart .= ','.$item; } else { $item = ''; $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; $cart = preg_replace("/_/i", " ", preg_replace("/(\d+)_(\d+)/i", "$1.$2", $cart)); break; } $_SESSION['cart'] = $cart; PHP: I tried adding global $cart; before the switch and did not work
I need to have a look at your entire file now. I have an hour to spare and will try and fix this up for you... Add me on MSN .
I have to say this is a very poor method of saving cart data. is there any reason you cant save it as a proper array?