This code compiles a dropdown list of all countries which you may use in forms. The first time it may take a second or two to compile the list, but after that the box will compile spontaneously from the cache.
1. Install the 'GeoNames API' and 'countryinfo' modules on your site.
2. Create a new page on your site, and set Input format to PHP code.
3. Add the following code to the page:
<?php
$result = geonames_query('countryinfo');
foreach ($result->results as $country) {
$optionlist .= sprintf('<option value="%s">%s</option>', $country['countrycode'], $country['countryname']);
}
?>
<form>
<select><? print $optionlist ?></select>
</form>
Note that the list is ordered by Country Code (we display only the Country Name, but the Country Code is used as value). To learn how to order the results, read the
Ordered Country Dropdown code example.
Explanation:
The geonames_query function uses the 'countryinfo' module to find the results. If no parameters are passed, all countries are returned. We loop through the results with foreach and generate some option HTML for a dropdown list (select list).
Then we display a form, and put the option list between two select tags, just to display our result.
Note: The first time you run the query, the server may use some time (some seconds) for the request. However, if you have the cache enabled - the country data will be retrieved from the cache on the next request (which is lightning fast!).
Recent Comments
23 weeks 2 days ago
30 weeks 1 day ago
1 year 18 weeks ago
2 years 8 weeks ago
2 years 12 weeks ago
2 years 12 weeks ago
2 years 32 weeks ago
2 years 38 weeks ago
2 years 38 weeks ago
2 years 40 weeks ago