0

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!

4 Answers 4

1

try this

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

}
}
echo '</select>';
Sign up to request clarification or add additional context in comments.

Comments

0

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>';
    }
   }

6 Comments

Hi jogesh_pi, thank you for the prompt response. Did compiled the suggested codes but value is still not displaying as default.
@user3237459 what is this $maxindex? it simply means the condition not match in if ($types[$maxindex] == $rows['officer']){ part, Try to debug it to echo the $types[$maxindex] and check what value your are matching with.
hello. yes it is matching. did an echo to verify its content. It is one of the matching selection found in $rows['officer'].
@user3237459 take a look on html source code or use firebug / developer's toolkit and verify that you get the value like <option selected="selected"> or not.
hi, retrieved from page source : <option selected="selected" >Mr John</option>. It is the value matching $types[$maxindex]. However, it is not shown as default highlighted when displaying the page.
|
0

You need add the selected attribute inside the <option> tag, like

<option selected='selected'>youroption</option>.

Try this,

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

    }
    echo '</select>';
}

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.