Yii 1.1: countrycodes

Autogenerate dropDownLists with country info: name, code, currency, ... with language support.

A simple component to generate dropDownLists with country info. Support for countryname, countrycode/isoalpha3, currency, capital ... in different languages.

Uses the extension egeonameservice to download country info data from http://www.geonames.org/export/web-services.html#countryInfo



  • Install the extension egeonameservice.
  • Extract 'ECountryCodes' into application/components.
$codes = new ECountryCodes;
echo $codes->dropDownList('codes1');
$codes->language = 'de'; // ISO-639-1 language code (en,de,fr,it,es,...) (default = english)
$codes->topKeys = array('AT','DE'); //move these countries to the top of the list
$codes->value = array('countryCode','currencyCode','countryName','capital');
echo $codes->dropDownList('codes2','DE');
$codes->selectKeys = array('AUT','DEU','LIE');
$codes->key = 'isoAlpha3';
$codes->value = array('isoAlpha3','countryName');
echo $codes->dropDownList('codes3');
$codes->excludeKeys = array('AT','DE','CH');
echo $codes->dropDownList('codes4','CK');
$codes->key = 'isoAlpha3';
$codes->value = array('isoAlpha3','countryName');
echo $codes->dropDownList('codes5','CK');
$codes->key = 'currencyCode';
$codes->value = array('currencyCode');
echo $codes->dropDownList('codes6');
$codes->update=true; //download the newest data, but bad performance
$codes->value = array('countryName','population');
echo $codes->dropDownList('codes7');

You can generate dropDownList for models too.

echo $codes->activeDropDownList($model,$attribute);

You can use combined keys too.

Supported attributes for keys and values:

private $_supportedAttributes = array(

Usage of the static method getCountryValue (v2.1):

If you have a countrycode activeDropDownList in your form with 'countryCode' as key, but you have to save the countrycode and the countryname in your model you can use this method in the controllers create/update action like this:

//$countryName is the countryname from countrycode 'AT' in german
$countryName = ECountryCodes::getCountryValue('countryCode','AT','countryName','de');
//In the controller
$model->countrycode = $_POST['Address']['countryCode']; //submitted from the activeDropDownList
$model->countryname = ECountryCodes::getCountryValue('countryCode',$model->countrycode,'countryName');
//If Yii::app()->language is a 2 letter ISO-639-1 language code
//you can add this as parameter language too.

Change log

  • version 2.0 New: Based on extension 'egeonameservice' Cancelled download from http://opencountrycodes.appspot.com because it only supports countrycode/name in english. Thanks to tonydspaniard for the component egeonameservice ... and ianare for his comment ;-)

  • version 2.1 Added static methods: instance, getCountryValue

Total 3 comments

#3373 report it
Joblo at 2011/04/06 08:25pm

Now it's more useful, hope so ...

#3367 report it
Joblo at 2011/04/06 12:58pm
The next todo

Yes I know it's very basic, it was only a quick hack for my actually needs.

With the recently released extension 'egeonameservice' (by tonydspaniard) you can do much more. All you have to do is to parse the json results and generate dropdownlists for your needs.

I think I will update this component based on 'egeonameservice' to support different languages, 3 letter codes etc.

#3362 report it
ianaré at 2011/04/06 11:10am
a little basic ...

This is handy but quite easy to do, I think having extra information would be beneficial.

Commonly needed, but difficult to acquire info : 3 letter ISO code, int'l calling code, postal code format, currency, official language(s), administrative divisions (states, regions), etc ...

Leave a comment

Please to leave your comment.

Create extension