0

I have the following where it generates options from the database

foreach($categories as $rowcat)
{echo '<option value="'.$rowcat->catname.'">'.$rowcat->catname.'</option>';
}

I need to select an option when loading, so how i'm gonna insert like

selected="Office"

2 Answers 2

1
$opt = "Office";
foreach($categories as $rowcat){                   
    if( $row->catname == $opt ) {
    echo '<option value="'.$row->catname.'" selected="selected">'.$rowcat->catname.'</option>';                
   }
   else {
    echo '<option value="'.$row->catname.'">'.$rowcat->catname.'</option>';
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1
foreach($categories as $rowcat){                   

echo '<option value="'.$rowcat->catname.'" '.($rowcat->catname=='Office') ? 'selected="selected"' : ''.'>'.$rowcat->catname.'</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.