I have a fairly simple problem. I have a select dropdown with values 1 to 10. This is an edit screen so users have already chosen a value that should be selected. I am giving them the chance to change it. So all I need to do is insert the word selected in the option value. To accomplish this simple task, I have been doing a bunch of inelegant if thens. Is there a best practice or simple way to do this. It seems unnecessary overhead to do all these if thens and create 10 new variables. Note for reasons of keeping html separate from php logic, I do not want to write the option values out sequentially I have the html in a separate area and just need to set the variable selected in the appropriate places.
if ($row['points']==1){
$select1 = "selected"}
if ($row['points']==2){
$select2 = "selected"}
//values 3 through 9
if ($row['points']==2){
$select2 = "selected"}
echo '<select name="points">';
echo '<option value = 1'. $select1.'>1</option>';
echo '<option value=2'. $select1.'>2</option>';
//values 3 through 9
echo '<option value=10'.$select10.'>10</option>';
echo '</select>';
Because you are using single selectable list (no multiple selected allowed) i.e. so only one can be selected at once.. look for http://fi.php.net/str_replace