Retrieve database fields in SELECT

Hi All!

I have a script at my site for editing user account details, among others the title of the user and the country.
The script shows the current values at the select option like :


<select name="title">
<option value="Mr" <? if ($user_title == "Mr") { ?> selected="selected" <? } ?>>Mr</option>
<option value="Miss <? if ($user_title == "Miss") { ?> selected="selected" <? } ?>">Miss</option>
<option value="Mrs" <? if ($user_title == "Mrs") { ?> selected="selected" <? } ?>>Mrs</option>
<option value="Prof" <? if ($user_title == "Prof") { ?> selected="selected" <? } ?>>Prof</option>
<option value="Mgr" <? if ($user_title == "Mgr") { ?> selected="selected" <? } ?>>Mgr</option>
<option value="Phd" <? if ($user_title == "Phd") { ?> selected="selected" <? } ?>>Phd</option>		
</select>

Now I would like to use this for the countries as well, however the countries list is much, much larger than the title’s… Is there a more elegant way to ‘select’ the current one from the database instead of writing the PHP line for every single country (there are around 140-150…)?

Thank you advanced!

Put the allowed values in an array. Loop through that array and display the options with the values added dynamically (through php) instead of hard coded.

Thank you for the tip guido!
Here’s my solution :


for ($i=1; $i<194; $i++) {
	echo "<option value='".$country_list[$i]."'";
	if ($country_list[$i] == $user_country) {
	echo "selected='selected'";
	}
	echo ">".$country_list[$i]."</option>";
	}

The country list is here :
http://www.ustrem.org/en/articles/array-list-countries-en/

Regards!

Thank you for the tip guido!
Here’s my solution :


for ($i=1; $i<194; $i++) {
	echo "<option value='".$country_list[$i]."'";
	if ($country_list[$i] == $user_country) {
	echo "selected='selected'";
	}
	echo ">".$country_list[$i]."</option>";
	}

The country list is here :
http://www.ustrem.org/en/articles/array-list-countries-en/

Regards!

That doesn’ look bad. But you might want to check out foreach to loop through an array.