I'd like to share with you these simple steps to create taxonomy based countries list that contain country name and code
1- Create new vocabulary with machine name "countries".

2- Add new field "field_country_code" to the vocabulary, which contain the country code.

3- Execute the following PHP code inside your theme template files or using Devel module.
<?php // Don't use <?php tags with Devel module
// We request this pretty txt file from www.iso.org which contain list of countries with code name
$countries_list_request = drupal_http_request("http://maged.me/country_names_and_code_elements.txt");
// Make sure we have requested the URL Successfully
if($countries_list_request->code == 200) {
$countries_list = explode("\n", $countries_list_request->data);
foreach($countries_list as $country_content) {
// Skip first row Country "Name;ISO 3166-1-alpha-2 code"
if(strpos($country_content, 'Country Name') !== FALSE) {
continue;
}
// Extract country name and code from the text
$country = explode(";", $country_content);
if(count($country) == 2) {
$country_name = $country[0] ;
$country_code = $country[1] ;
// Create new taxonomy term of type "countries" and field "field_country_code"
// Replace "countries" with vocabulary machine name
$countries_vocabulary = taxonomy_vocabulary_machine_name_load("countries") ;
$term = new stdClass();
$term->name = $country_name;
$term->vid = $countries_vocabulary->vid;
// Replace "field_country_code" with country code field name
$term->field_country_code[LANGUAGE_NONE][0]['value'] = $country_code ;
taxonomy_term_save($term);
}
}
}
?> // Skip this line with Devel moduleAaaand finally...We have the list of countries :)

Good Luck,
Maged Eladawy



