dragon_sa Posted December 19, 2010 Share Posted December 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222128-why-does-this-only-select-last-option/ Share on other sites More sharing options...
BlueSkyIS Posted December 19, 2010 Share Posted December 19, 2010 echo the HTML to see. I suspect either every option is getting selected='selected', or only the last one. Quote Link to comment https://forums.phpfreaks.com/topic/222128-why-does-this-only-select-last-option/#findComment-1149248 Share on other sites More sharing options...
dragon_sa Posted December 19, 2010 Author Share Posted December 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222128-why-does-this-only-select-last-option/#findComment-1149319 Share on other sites More sharing options...
BlueSkyIS Posted December 19, 2010 Share Posted December 19, 2010 therefore ($sizes==$size) is always true. echo these values and see what they are. continue backwards until you figure out why one of them isn't the value you expect. Quote Link to comment https://forums.phpfreaks.com/topic/222128-why-does-this-only-select-last-option/#findComment-1149354 Share on other sites More sharing options...
PFMaBiSmAd Posted December 19, 2010 Share Posted December 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222128-why-does-this-only-select-last-option/#findComment-1149358 Share on other sites More sharing options...
dragon_sa Posted December 20, 2010 Author Share Posted December 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222128-why-does-this-only-select-last-option/#findComment-1149441 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.