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.

Member Avatar for diafol
$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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.