Question

I have this PHP code that I use to make a dropdown form selection of country's I would like to eliminate this extra mysql query though and just store the output like in code to on the page However I am lost on how I would have the users country SELECTED If I do not use the query to get the data Please advice

<select name="country"  style="width:180px;" onChange="do_get_rest_popup(this.value)" o>
<?PHP
$sql="SELECT * FROM  users_countries  ORDER BY country";
$result_country = executequery($sql);   
while($line_country = mysql_fetch_array($result_country)){
   $db_country_id = $line_country['id']; 
   $country_name = $line_country['country'];
?>
<option value="<?=$db_country_id?>"<? if($line_member['country']==$db_country_id){ echo " SELECTED";} ?>><?=$country_name?></option>
<?
}
?>
</select>

Code about outputs this on page, I have strunk the quantity of countries down for this post

<select name="country"  style="width:180px;" onChange="do_get_rest_popup(this.value)" o> 
<option value="217">Turkmenistan</option> 
<option value="218">Turks and Caicos Islands</option> 
<option value="219">Tuvalu</option> 
<option value="220">Uganda</option> 
<option value="221">Ukraine</option> 
<option value="222">United Arab Emirates</option> 
<option value="223">United Kingdom (Great Britain)</option> 
<option value="224" SELECTED>United States</option> 
</select>
Was it helpful?

Solution

How about something like this?

<?
$countries = array(
"217" => "Turkenistan",
"218" => "Turks and Caicos Islands",
"219" => "Tuvalu",
"220" => "Uganda",
"221" => "Ukraine",
"222" => "United Arab Emirates",
"223" => "United Kingdom (Great Britain)"
"224" => "United States");
?>

<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" /> 
<?php
$countryCounter = 1;
$amtOfCountries = count($countries);
foreach ($country as $id => $c) {
    if ($countryCounter == $amtOfCountries) { 
    echo "<option value=\"$id\" SELECTED>$c</option>";
    } 
    else {
    echo "<option value=\"$id\">$c</option>";
    $countryCounter++;
        }
}
?>
</select>

EDIT: I did not test this but you should get the idea

OTHER TIPS

You could:

  • print an array of countries as PHP code using var_export(), and then hardcode that somewhere.
  • Cache the values from the database using something like Pear Cache_Lite - this is very straightforward to use and it means that if you modify the values in the database, all you have to do is delete the cache file to have it regenerated.

With both the above options, you then have an array which you can loop over in a similar way to what you are doing now in order to generate the html.

You could buffer the static html and do a simple string replace for the selected value.

1) Save the country HTML list into countries.html

2) At load time, read countries.html into a variable and do a string replace:

$countries = file_get_contents('countries.html');    
echo str_replace('value="'.$userCountry.'"','value="'.$userCountry.'" SELECTED',$countries);

Not very memory efficient overall, but it does save the database hit and processing time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top