Jump to content

why does this only select last option


dragon_sa

Recommended Posts

I am adding to a shopping cart using sessions, when I add a product with a selected size it echoes the correct size as text but when applying it to my option boxes as selected, it just shows the last item in the select box.

here is the code for the session variables

<?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)); }
?>

 

and this bit inside the same loop displayes the select box for size

<?php if ($sizelist && $sizelist='add') { echo "<select name='size' id='size' style='vertical-align:middle;'>"; foreach($sizepick AS $sizes) { if ($sizes==$size) { echo "<option value='$size' selected='selected'>$size</option>"; } else { echo "<option value='$sizes'>$sizes</option>"; }} echo "</select>"; } else { echo $size; } ?>

 

appreciate any help on this

Link to comment
Share on other sites

echo the HTML to see. I suspect either every option is getting selected='selected', or only the last one.

 

It is showing every thing is selected for some reason?

 

$ses_basket_size is only the users selected size from th eprevious page, $ses_basket_sizechoice is the size options available for that item, I cant quite see why its doing this.

Link to comment
Share on other sites

You have at least two logic errors in the code you HAVE posted -

 

if ($country='AU')

 

and

 

if($sizelist && $sizelist='add')

 

You are setting $country equal to 'AU' not testing if it is 'AU' and you are setting $sizelist equal to 'add' not testing if it is 'add' and I suspect that your current problem in the actual code that is using $size and/or $sizes is probably doing the same thing.

Link to comment
Share on other sites

doh!!, I didnt even realize I had done that, thank you for pointing that out PFMaDiSmAd, problem solved, setting it as follows did the trick

 

if ($sizelist && $sizelist=='add')

 

and

 

if ($sizes==$size)

 

I also changed the country bit to as that adds the GST applied to australia.

 

cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.