max_power Posted March 5, 2010 Share Posted March 5, 2010 Hi all, Within in an editing form, how does one make sure that the option that was already selected in drop down list doesn't appear twice (as a selected option and as a normal option) So something like: Edit form: option 1 – selected option (say the value is a) option 3 – b option 4 – c and not this: option 1 – selected option (say the value is a) option 2 – a option 3 – b option 4 – c Thanks Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/ Share on other sites More sharing options...
harristweed Posted March 5, 2010 Share Posted March 5, 2010 Where are the options coming from? Is the list dynamically generated? Lets see some code. Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1021846 Share on other sites More sharing options...
max_power Posted March 5, 2010 Author Share Posted March 5, 2010 Yep, it is dynamically generated from a mySQL db. Here is the code: <tr> <td width="150" class="label">Relationship</td> <td class="content"> <select name="relationship" id="relationship"> <option selected="selected"><?php echo $relationship; ?></option> <?php $sqlQuery = "select relationship_id, name from tbl_relationship"; $result = mysql_query($sqlQuery); while($row = mysql_fetch_assoc($result)) { $id = $row["relationship_id"]; $name = $row["name"]; echo "<option>$name</option>\n"; } ?> </select> </td> $relationship being the variable. Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1021903 Share on other sites More sharing options...
greatstar00 Posted March 5, 2010 Share Posted March 5, 2010 try this $sqlQuery = "select relationship_id, distinct name from tbl_relationship"; Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1021905 Share on other sites More sharing options...
max_power Posted March 6, 2010 Author Share Posted March 6, 2010 Thanks for the reply. But it doesn't work, it only shows the existing chosen option and that's it within the drop down list. Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022222 Share on other sites More sharing options...
max_power Posted March 6, 2010 Author Share Posted March 6, 2010 I got it to work another way but I have a similar problem for a static dropdown list.. Would anyone know how to ensure the selected option doesnt appear twice for the hard coded option values, when editing the form? The code is: <select name="state" id="state" class="box"> <option selected="selected" ><?php echo $state; ?></option> <option>ACT</option> <option>NSW</option> <option>Victoria</option> <option>WA</option> <option>SA</option> <option>Tasmania</option> <option>NT</option> <option>Queensland</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022285 Share on other sites More sharing options...
cags Posted March 6, 2010 Share Posted March 6, 2010 $states = array('ACT', 'NSW', 'Victoria'); // this is just to indicate don't mix your current $state value with the one in the foreach loop $selected_state = $state; foreach($states as $state) { if($state == $selected_state) { echo '<option selected="selected">'; } else { echo '<option>'; } echo $state . '</option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022300 Share on other sites More sharing options...
anirudhtomer Posted March 6, 2010 Share Posted March 6, 2010 SINCE the first item is selected by default so the following statement makes the first option repeat twice <option selected="selected" ><?php echo $state; ?></option> //other options down in the list so try removing the line <option selected="selected" ><?php echo $state; ?></option> & keep the other options Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022304 Share on other sites More sharing options...
max_power Posted March 7, 2010 Author Share Posted March 7, 2010 Thanks for all the replies. All solved! Quote Link to comment https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022566 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.