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

  1. Requirements
  2. Usage
  3. Change log

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



  • 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 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

6 0
1 739 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: User Interface
Developed by: Joblo
Created on: Apr 4, 2011
Last updated: 7 years ago


show all

Related Extensions