views:

222

answers:

1

I have a drop down menu that I am dynamically populating based on values in a database table. The code looks something like this:

<select>
<option value="" selected="selected">Select Option</option>
 <?php foreach($var as $row): ?>
  <option value="<?php echo $row->value;?>"><?php echo $row->value;?></option>
 <?php endforeach;?>
</select>

This works fine and creates a default option called 'Select Option' at the top of the list. But I also have another variable that I am passing to this view that specifies a custom default option. So rather than having 'Select Option' as the default selected option, it would be one of the dynamically printed values in the middle of the list? Any ideas on how to do this (no javascript)?

+2  A: 

check with an if-else construct in your foreach if the current option is the default one. If so, also print selected="selected"

Zed
don't forget to remove the selected="selected" from your top option in this case.
Scharrels