Ell20 Posted March 30, 2008 Share Posted March 30, 2008 Hi, I have a dynamic drop down box which lists the names of users from the database to select from then once selected it posts the user_id of the user into the database. However when I then edit this value I would like the drop down box to display the current value that is stored in the database, how would I go about doing this? Here is my current code: <select name="officer"><option value="">Select Officer:</option> <?php $sql = "SELECT * FROM users WHERE club_id = '$id' AND member_type != 'Guest'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)){ $user_id = $row["user_id"]; $first_name = $row['first_name']; $last_name = $row['last_name']; $name = "$first_name $last_name"; ?> <option value="<?php echo $user_id ?>"><?php echo $name ?></option> <?php } ?> </select> Thanks for your help Link to comment https://forums.phpfreaks.com/topic/98625-dynamic-drop-down-box-show-current/ Share on other sites More sharing options...
darkhappy Posted March 30, 2008 Share Posted March 30, 2008 you have to store the user_id that you put into the database into a separate variable, call it $usr_select or something and then: if($usr_select == $user_id) { ?> <option value="<?php echo $user_id ?>" SELECTED ><?php echo $name ?></option> <?php } else { ?> <option value="<?php echo $user_id ?>"><?php echo $name ?></option> <?php } so once you post the original selection and store it into the user_select variable, you just re-create the select list, adding the "SELECTED" option if the originally stored variable matches one of the user id's in the array - which puts it at the top of the list in the drop down box. Let me know if this does not make sense! - dhappy Link to comment https://forums.phpfreaks.com/topic/98625-dynamic-drop-down-box-show-current/#findComment-504761 Share on other sites More sharing options...
Ell20 Posted March 30, 2008 Author Share Posted March 30, 2008 I get what your saying but at the moment the SQL statement just pulls out all the names that are avaliable, so do I need another SQL statement to get the current value from which I then create the new variable?? Cheers Link to comment https://forums.phpfreaks.com/topic/98625-dynamic-drop-down-box-show-current/#findComment-504831 Share on other sites More sharing options...
darkhappy Posted March 30, 2008 Share Posted March 30, 2008 no i wouldn't even bother with that - just do something like this before the select list (assuming your form is posting back to self? i do not see that in the original code snippet): if(isset($_POST(['officer'])) { $user_select = $_POST['officer']; } Link to comment https://forums.phpfreaks.com/topic/98625-dynamic-drop-down-box-show-current/#findComment-504865 Share on other sites More sharing options...
Ell20 Posted March 30, 2008 Author Share Posted March 30, 2008 I used another select statement in the end. But its working so thats the main thing. Thanks for your help Link to comment https://forums.phpfreaks.com/topic/98625-dynamic-drop-down-box-show-current/#findComment-504867 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.