Reputation: 337
I'm currently outputting a select menu using the following code;
while($row = mysqli_fetch_array($result) ) {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
I'm wondering how I can make the one that is selected in the database appear as selected, seeings as the options are dynamically drawn and added?
Edit: I still want the other options that aren't selected to appear in the list so it can be changed.
Upvotes: 1
Views: 78
Reputation: 2934
Make an if condition and then use selected
Attribute
while($row = mysqli_fetch_array($result) ) {
if (your condition) {
echo '<option value="'.$row['id'].'" selected>'.$row['title'].'</option>';
} else {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
}
Upvotes: 1
Reputation: 3299
You try this way:
while($row = mysqli_fetch_array($result) ) {
$selected=$row['id']==$current_id? "selected": ""; //$current_id is which you want to selected..
echo '<option value="'.$row['id'].'" '. $selected.' >'.$row['title'].'</option>';
}
Upvotes: 2