turpentyne Posted February 26, 2012 Share Posted February 26, 2012 I must be missing something simple. I've got this little script that pulls rows from the database to populate a dropdown. If one of them matches a predetermined variable, then I want it to show selected. It's... almost working. The dropdown prints, and shows the options. But the selected item shows separate, printed just below the dropdown? Here's what I've got: <?php $quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount"); echo " <select name='discount_id'><option value=''>Select</option>"; while($row = mysql_fetch_array($quer3)) { if($row[discount_id]==$discount_id){echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]." / $".$row[discount_amount]."</option>";} else{echo "<option value='$row[discount_id]'>$row[discount_amount]</option>";} echo "</select>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257836-dropdown-selected-not-showing-right/ Share on other sites More sharing options...
freelance84 Posted February 27, 2012 Share Posted February 27, 2012 The only difference between the two possible outcomes is that the selected has an additional forward slash within the option. Could you post the html which is outputted? Quote Link to comment https://forums.phpfreaks.com/topic/257836-dropdown-selected-not-showing-right/#findComment-1321544 Share on other sites More sharing options...
Pikachu2000 Posted February 27, 2012 Share Posted February 27, 2012 Where is $discount_id even defined? Quote Link to comment https://forums.phpfreaks.com/topic/257836-dropdown-selected-not-showing-right/#findComment-1321584 Share on other sites More sharing options...
givememore Posted February 27, 2012 Share Posted February 27, 2012 As I see, the problem is the echo "</select>"; inside the while loop. Try to put it after the loop ending }: <?php $quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount"); echo " <select name='discount_id'><option value=''>Select</option>"; while($row = mysql_fetch_array($quer3)) { if($row[discount_id]==$discount_id){echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]." / $".$row[discount_amount]."</option>";} else{echo "<option value='$row[discount_id]'>$row[discount_amount]</option>";} } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/257836-dropdown-selected-not-showing-right/#findComment-1321621 Share on other sites More sharing options...
freelance84 Posted February 27, 2012 Share Posted February 27, 2012 As I see, the problem is the echo "</select>"; inside the while loop. Try to put it after the loop ending }: <?php $quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount"); echo " <select name='discount_id'><option value=''>Select</option>"; while($row = mysql_fetch_array($quer3)) { if($row[discount_id]==$discount_id){echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]." / $".$row[discount_amount]."</option>";} else{echo "<option value='$row[discount_id]'>$row[discount_amount]</option>";} } echo "</select>"; ?> Ahhh... missed that! Turnpentine, I often find that laying out my code makes it 100000000000 times quicker to bug find later. Not doing so for what ever reason mean you will look over simple little mistake. For example your op: <?php $quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount"); echo " <select name='discount_id'><option value=''>Select</option>"; while($row = mysql_fetch_array($quer3)) { if($row[discount_id]==$discount_id) { echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]." / $".$row[discount_amount]."</option>"; } else{ echo "<option value='$row[discount_id]'>$row[discount_amount]</option>"; } echo "</select>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257836-dropdown-selected-not-showing-right/#findComment-1321623 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.