dragon_sa Posted December 24, 2010 Share Posted December 24, 2010 When I remove 1 session item using this it works fine when I select more than 1 item to be removed I get unexpected results, I have checked the outputted html to see what positions get echoed into the remove check box and they are correct, so I guess the problem lies in my remove code here is the part of the form that sends the remove check box <?php for ($basket_counter=0;$basket_counter<$_SESSION['ses_basket_items'];$basket_counter++) { $price=sprintf("%01.2f",$ses_basket_price[$basket_counter]); $quantity=$ses_basket_amount[$basket_counter]; $code=$ses_basket_stockcode[$basket_counter]; $itemID=$ses_basket_id[$basket_counter]; $name=$ses_basket_name[$basket_counter]; $image=$ses_basket_image[$basket_counter]; $size=$ses_basket_size[$basket_counter]; $sizechoice=$ses_basket_sizechoice[$basket_counter]; // create array from sizes and make the option box selections if ($sizechoice!="" && (!in_array($sizechoice, array('One Size', 'one size', 'free size', 'Free Size', 'Adjustable', 'adjustable')))) { // add size trigger $sizelist="add"; //create the array $sizepick=explode(",", $sizechoice); } else { $sizepick=$sizechoice; } // adjust gst for AU customers if ($country=='AU') { $price=sprintf("%01.2f",($price*1.1)); $unit=sprintf("%01.2f",($price/$quantity)); } else { $unit=sprintf("%01.2f",($price/$quantity)); } ?> <tr> <td align='center' class='rescon' style="border-bottom:solid #330000 1px;"><input type="checkbox" name="remove[<?php echo $basket_counter; ?>]" value="<?php echo $basket_counter; ?>" /></td> and here is the part of the code that processes the removal of the items $remove = $_POST['remove']; if (isset($remove) && ($remove!='')) { foreach($remove AS $key => $remove_position){ array_splice ($ses_basket_name, $remove_position, 1); array_splice ($ses_basket_amount, $remove_position, 1); array_splice ($ses_basket_price, $remove_position, 1); array_splice ($ses_basket_stockcode, $remove_position, 1); array_splice ($ses_basket_image, $remove_position, 1); array_splice ($ses_basket_size, $remove_position, 1); array_splice ($ses_basket_sizechoice, $remove_position, 1); array_splice ($ses_basket_id, $remove_position, 1); $_SESSION['ses_basket_items']--; } } I am using a form with checkboxes named remove[$remove_position] and the positions in the form are correct for each item in the array, but when I select more than 1 item to delete they are not always the correct items only the first 1 selected. What am I doing wrong here?? Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted December 24, 2010 Author Share Posted December 24, 2010 I am suspecting that the issue may be as each time it loops the item positions change and then the set $remove_positions become incorrect, if this is the case how would I get around this to remove all items selected at once?? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 24, 2010 Share Posted December 24, 2010 have you considered storing basket information in a database instead of using manipulation of an array? if you have one record for each item in the basket, all you have to do is have a link to delete an item id, then remove the item with that id from the database. Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted December 24, 2010 Author Share Posted December 24, 2010 For me that would be an easy way but I want to reduce the number of calls to the database. I think sessions are good way to store temporary information before its entered into the database. Quote Link to comment Share on other sites More sharing options...
the182guy Posted December 25, 2010 Share Posted December 25, 2010 Don't have a seperate array for every attribute of a cart item, just have one multidimensional array like $_SESSION['CART']['ITEMS'] = array(); // add an item $_SESSION['CART']['ITEMS'][] = array( 'product_id' => '1', 'price' => 50, 'stockcode' => '001' ); This way you can easily loop through the cart items like [code=php:0] foreach($_SESSION['CART']['ITEMS'] as $item) { echo $item['product_id']; echo $item['stockcode']; } // or to delete an item from the cart $index = 0; // item index of the array to delete, 0=first item, 1=second item etc unset($_SESSION['CART']['ITEMS'][$index]); [/code] Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted December 25, 2010 Author Share Posted December 25, 2010 How would that go for deleting multiple items in a loop, as the problem I am having now seems to be the $index number changes for each item after removal then the index numbers I have tagged for removal after that are for the wrong items. eg 10 items in the cart index 0-9 I send the trigger to remove 4, 7 and 9 4 removes fine on first loop but 8 becomes 7 and is then removed on second loop then on the third loop 9 has also become 7 so nothing gets removed as it is now the last item and there is no 9 to remove Quote Link to comment Share on other sites More sharing options...
the182guy Posted December 25, 2010 Share Posted December 25, 2010 The index number won't change when using the multidimensional array, even if items are removed all other items will remain unchanged so if item 3 is removed then the indexes remaining are 1, 2, 4, 5 etc Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted December 25, 2010 Author Share Posted December 25, 2010 Rather than recoding several pages which call the session variables I found a bit of trickery using the onclick event on a checkbox. <input type="checkbox" name="remove[<?php echo $basket_counter; ?>]" value="<?php echo $basket_counter; ?>" onclick="document.formName.submit();" /> So now each time a checkbox is ticked it submits the form and removes that one item, then I can use the update cart button to modify the values of other session variables all at once using the current index system of the sessions. 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.