tags:

views:

548

answers:

3

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>
+1  A: 

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.

Tom Haigh
A: 

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.

zombat
why do you need the output buffering? file_get_contents() shouldn't output anything
Tom Haigh
Hah, you're right of course. I was mixing up my code fragments! I'll fix it.
zombat
+2  A: 

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

rodey
awsome I will try this! Another reason I wanted to eliminate the DB call beside performance is I would like to possibly make USA Canada and UK show up at the top of the list above all countries for easiar selection for the majority of the users, do you know how I could add that in
jasondavis
I tried this, I can't get it to populate the form with any data though, i'm not very good with arrays so I am lost on getting it to work
jasondavis
The order they appear in the array is the order they will appear in the drop down list so you would need to re-order the values in the array when declaring the array. And I already see in error in what I posted - in my foreach statement $county should be $countries. I don't have access to a webserver or I would test it.
rodey