php - Looping through a drop down, how to select value? -
when displaying form on page user edit information, , form consists of drop down box, how loop through selections in dropdown box select predefined mysql entry?
for example
users country: australia
how go searching through list of countries ie: http://snipplr.com/view/4792/country-drop-down-list-for-web-forms/ make:
<option value="au">australia</option>
become
<option value="au" selected="selected">australia</option>
you like:
<?php $countries = array('au' => 'australia', 'af' => 'afghanistan', ...); $selected = 'au'; foreach ($countries $code => $label) { echo '<option value="' . $code . '"'; if ($selected == $code) { echo ' selected="selected"'; } echo '>' . $label . '</option>'; } ?>
not prettiest idea. shakti suggests, it's easier maintain if values in db , not in massive array in middle of code.
Comments
Post a Comment