Jump to content

remove session array works for 1 but not 2 items


dragon_sa

Recommended Posts

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??

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??

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.

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]

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.