0

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."'
>".$getCategory."</option>";
              }
              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

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by HelenLF
0
$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.

0

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.