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
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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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