Jump to content

Dynamic Drop Down Box Show Current


Ell20

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

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