I wonder if this can be done.
I have a select drop down box where the options are populated form a field in a database table something like this.

  $SQL = "SELECT DISTINCT category FROM atable ORDER BY category";
              $result = mysql_query($SQL);              
              echo "<select name='addCategory' size='6'>";             
              while ($db_field = mysql_fetch_assoc($result)) 
              $getCategory =  ($db_field['category']);               
              echo "<option value='".$getCategory."'
              echo "</select>";

What I want to do is have one of the values of the drop down box to be pre-selected if it matches an entry in a column in another table. Can anyone tell me how to do this or even if it can be done? I am a php beginner.

Edited by Nick Evan: Fixed formatting

8 Years
Discussion Span
Last Post by HelenLF
$theValueToCheck = ...(whatever)...
while ($db_field = mysql_fetch_assoc($result)) 
  $getCategory = stripslashes($db_field['category']);
  $sel = "";
  if($getCategory == $theValueToCheck)$sel = ' selected="selected"'; 
  echo "<option value='{$getCategory}'{$sel}>{$getCategory}</option>";

Sorry a bit messy, but I think it should work.


It did work. Thank you so much - I've been staring at this for ages.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.