PHP drop-down menu with the value selected by default

advertisements

I am working on a PHP project as follows

 if( mysql_num_rows( $result ) > 0 ) {
    echo '<select name="officerlist">';
    while ( $rows = mysql_fetch_array ( $result ) ) {
       echo '<option>' . $rows['officer'] . '</option>';
       if ($types[ $maxindex ] == $rows['officer']){
        echo ' selected';
       }
    }
        echo '</select>';
}

I am able to populate the query result into the dropdown menu. However, I want to implement it to have a default selected value based on value from another array. (ie if $types[$maxindex] is found in $rows['officer'], it will auto select the value.).

Anyone able to advise?

Thank you and best regards!


should be like this,

while ( $rows = mysql_fetch_array ($result) ) {                       

    if ($types[$maxindex] == $rows['officer']){
        echo '<option selected="selected" value="'.$rows['officer'].'">' . $rows['officer'] . '</option>';
    } else {
       echo '<option value="'.$rows['officer'].'">' . $rows['officer'] . '</option>';
    }
   }