Skip to content

helper dropdown country code

bradym edited this page Jun 27, 2014 · 10 revisions

Country codes are defined by the International Standard ISO 3166-1-alpha-2. When writing an application that has to recognise and handle different countries, it's handy to have a drop-down helper for users to nominate a country.

There are two useful components on this page:

  • the country code array (which you can easily use for your own applications) and
  • a helper to generates the HTML for a drop-down box, and takes a few parameters.

Watermark Studios originally published a country code array in thread 141781 on 2010-01-17:

$config['country_list'] = array(
"AF"=>"Afghanistan",
"AL"=>"Albania",
"DZ"=>"Algeria",
"AD"=>"Andorra",
"AO"=>"Angola",
"AI"=>"Anguilla",
"AQ"=>"Antarctica",
"AG"=>"Antigua and Barbuda",
"AR"=>"Argentina",
"AM"=>"Armenia",
"AW"=>"Aruba",
"AU"=>"Australia",
"AT"=>"Austria",
"AZ"=>"Azerbaijan",
"BS"=>"Bahamas",
"BH"=>"Bahrain",
"BD"=>"Bangladesh",
"BB"=>"Barbados",
"BY"=>"Belarus",
"BE"=>"Belgium",
"BZ"=>"Belize",
"BJ"=>"Benin",
"BM"=>"Bermuda",
"BT"=>"Bhutan",
"BO"=>"Bolivia",
"BA"=>"Bosnia and Herzegovina",
"BW"=>"Botswana",
"BR"=>"Brazil",
"IO"=>"British Indian Ocean",
"BN"=>"Brunei",
"BG"=>"Bulgaria",
"BF"=>"Burkina Faso",
"BI"=>"Burundi",
"KH"=>"Cambodia",
"CM"=>"Cameroon",
"CA"=>"Canada",
"CV"=>"Cape Verde",
"KY"=>"Cayman Islands",
"CF"=>"Central African Republic",
"TD"=>"Chad",
"CL"=>"Chile",
"CN"=>"China",
"CX"=>"Christmas Island",
"CC"=>"Cocos (Keeling) Islands",
"CO"=>"Colombia",
"KM"=>"Comoros",
"CD"=>"Congo, Democratic Republic of the",
"CG"=>"Congo, Republic of the",
"CK"=>"Cook Islands",
"CR"=>"Costa Rica",
"HR"=>"Croatia",
"CY"=>"Cyprus",
"CZ"=>"Czech Republic",
"DK"=>"Denmark",
"DJ"=>"Djibouti",
"DM"=>"Dominica",
"DO"=>"Dominican Republic",
"TL"=>"East Timor",
"EC"=>"Ecuador",
"EG"=>"Egypt",
"SV"=>"El Salvador",
"GQ"=>"Equatorial Guinea",
"ER"=>"Eritrea",
"EE"=>"Estonia",
"ET"=>"Ethiopia",
"FK"=>"Falkland Islands (Malvinas)",
"FO"=>"Faroe Islands",
"FJ"=>"Fiji",
"FI"=>"Finland",
"FR"=>"France",
"GF"=>"French Guiana",
"PF"=>"French Polynesia",
"GA"=>"Gabon",
"GM"=>"Gambia",
"GE"=>"Georgia",
"DE"=>"Germany",
"GH"=>"Ghana",
"GI"=>"Gibraltar",
"GR"=>"Greece",
"GL"=>"Greenland",
"GD"=>"Grenada",
"GP"=>"Guadeloupe",
"GT"=>"Guatemala",
"GN"=>"Guinea",
"GW"=>"Guinea-Bissau",
"GY"=>"Guyana",
"HT"=>"Haiti",
"HN"=>"Honduras",
"HK"=>"Hong Kong",
"HU"=>"Hungary",
"IS"=>"Iceland",
"IN"=>"India",
"ID"=>"Indonesia",
"IE"=>"Ireland",
"IL"=>"Israel",
"IT"=>"Italy",
"CI"=>"Ivory Coast (Côte d\'Ivoire)",
"JM"=>"Jamaica",
"JP"=>"Japan",
"JO"=>"Jordan",
"KZ"=>"Kazakhstan",
"KE"=>"Kenya",
"KI"=>"Kiribati",
"KR"=>"Korea, South",
"KW"=>"Kuwait",
"KG"=>"Kyrgyzstan",
"LA"=>"Laos",
"LV"=>"Latvia",
"LB"=>"Lebanon",
"LS"=>"Lesotho",
"LI"=>"Liechtenstein",
"LT"=>"Lithuania",
"LU"=>"Luxembourg",
"MO"=>"Macau",
"MK"=>"Macedonia, Republic of",
"MG"=>"Madagascar",
"MW"=>"Malawi",
"MY"=>"Malaysia",
"MV"=>"Maldives",
"ML"=>"Mali",
"MT"=>"Malta",
"MH"=>"Marshall Islands",
"MQ"=>"Martinique",
"MR"=>"Mauritania",
"MU"=>"Mauritius",
"YT"=>"Mayotte",
"MX"=>"Mexico",
"FM"=>"Micronesia",
"MD"=>"Moldova",
"MC"=>"Monaco",
"MN"=>"Mongolia",
"ME"=>"Montenegro",
"MS"=>"Montserrat",
"MA"=>"Morocco",
"MZ"=>"Mozambique",
"NA"=>"Namibia",
"NR"=>"Nauru",
"NP"=>"Nepal",
"NL"=>"Netherlands",
"AN"=>"Netherlands Antilles",
"NC"=>"New Caledonia",
"NZ"=>"New Zealand",
"NI"=>"Nicaragua",
"NE"=>"Niger",
"NG"=>"Nigeria",
"NU"=>"Niue",
"NF"=>"Norfolk Island",
"NO"=>"Norway",
"OM"=>"Oman",
"PK"=>"Pakistan",
"PS"=>"Palestinian Territory",
"PA"=>"Panama",
"PG"=>"Papua New Guinea",
"PY"=>"Paraguay",
"PE"=>"Peru",
"PH"=>"Philippines",
"PN"=>"Pitcairn Island",
"PL"=>"Poland",
"PT"=>"Portugal",
"QA"=>"Qatar",
"RE"=>"Réunion",
"RO"=>"Romania",
"RU"=>"Russia",
"RW"=>"Rwanda",
"SH"=>"Saint Helena",
"KN"=>"Saint Kitts and Nevis",
"LC"=>"Saint Lucia",
"PM"=>"Saint Pierre and Miquelon",
"VC"=>"Saint Vincent and the Grenadines",
"WS"=>"Samoa",
"SM"=>"San Marino",
"ST"=>"São Tome and Principe",
"SA"=>"Saudi Arabia",
"SN"=>"Senegal",
"RS"=>"Serbia",
"CS"=>"Serbia and Montenegro",
"SC"=>"Seychelles",
"SL"=>"Sierra Leon",
"SG"=>"Singapore",
"SK"=>"Slovakia",
"SI"=>"Slovenia",
"SB"=>"Solomon Islands",
"SO"=>"Somalia",
"ZA"=>"South Africa",
"GS"=>"South Georgia and the South Sandwich Islands",
"ES"=>"Spain",
"LK"=>"Sri Lanka",
"SR"=>"Suriname",
"SJ"=>"Svalbard and Jan Mayen",
"SZ"=>"Swaziland",
"SE"=>"Sweden",
"CH"=>"Switzerland",
"TW"=>"Taiwan",
"TJ"=>"Tajikistan",
"TZ"=>"Tanzania",
"TH"=>"Thailand",
"TG"=>"Togo",
"TK"=>"Tokelau",
"TO"=>"Tonga",
"TT"=>"Trinidad and Tobago",
"TN"=>"Tunisia",
"TR"=>"Turkey",
"TM"=>"Turkmenistan",
"TC"=>"Turks and Caicos Islands",
"TV"=>"Tuvalu",
"UG"=>"Uganda",
"UA"=>"Ukraine",
"AE"=>"United Arab Emirates",
"GB"=>"United Kingdom",
"US"=>"United States",
"UM"=>"United States Minor Outlying Islands",
"UY"=>"Uruguay",
"UZ"=>"Uzbekistan",
"VU"=>"Vanuatu",
"VA"=>"Vatican City",
"VE"=>"Venezuela",
"VN"=>"Vietnam",
"VG"=>"Virgin Islands, British",
"WF"=>"Wallis and Futuna",
"EH"=>"Western Sahara",
"YE"=>"Yemen",
"ZM"=>"Zambia",
"ZW"=>"Zimbabwe");

Watermark Studios published, in thread 141730, the following code that can be used within a helper.

The helper function takes four parameters:

  1. Select name - the , defaults to 'country'
  2. Id or class not required
  3. The selected country (e.g. from a user account 'GB') not wanted leave as ''
  4. Top countries to show in list (array)
  5. All option (e.g. every country can access this page OR can deliver to every country) not wanted leave as ''
  6. Selection country (e.g. 'Australia') selects the country for the user
  7. Show all countries (TRUE = show all, FALSE = only show top countries)

Example usage:

echo country_dropdown('country', 'cont', 'dropdown col_12_16', 'GB', array('US','CA','GB'), '');
// Returns a list of ALL countries, with an id of cont adds the classes of dropdown and col_12_16 the selected country shown at the top of the list and the three most common countries shown below and the all countries option is not shown because of no parameter ''

And the helper code is thus:

//selected country would be retrieved from a database or as post data
function  country_dropdown($name, $id, $class, $selected_country,$top_countries=array(), $all, $selection=NULL, $show_all=TRUE ){
	// You may want to pull this from an array within the helper
	$countries = config_item('country_list');

	$html = "<select name='{$name}' id='{$id}' class='{$class}'>";
	$selected = NULL;
	if(in_array($selection,$top_countries)){
		$top_selection = $selection;
		$all_selection = NULL;
	}else{
		$top_selection = NULL;
		$all_selection = $selection;
	}
	if(!empty($selected_country)&&$selected_country!='all'&&$selected_country!='select'){
		$html .= "<optgroup label='Selected Country'>";
		if($selected_country === $top_selection){
			$selected = "SELECTED";
		}
		$html .= "<option value='{$selected_country}'{$selected}>{$countries[$selected_country]}</option>";
		$selected = NULL;
		$html .= "</optgroup>";
	}else if($selected_country=='all'){
		$html .= "<optgroup label='Selected Country'>";
		if($selected_country === $top_selection){
			$selected = "SELECTED";
		}
		$html .= "<option value='all'>All</option>";
		$selected = NULL;
		$html .= "</optgroup>";
	}else if($selected_country=='select'){
		$html .= "<optgroup label='Selected Country'>";
		if($selected_country === $top_selection){
			$selected = "SELECTED";
		}
		$html .= "<option value='select'>Select</option>";
		$selected = NULL;
		$html .= "</optgroup>";
	}
	if(!empty($all)&&$all=='all'&&$selected_country!='all'){
		$html .= "<option value='all'>All</option>";
		$selected = NULL;
	}
	if(!empty($all)&&$all=='select'&&$selected_country!='select'){
		$html .= "<option value='select'>Select</option>";
		$selected = NULL;
	}
	
	if(!empty($top_countries)){
		$html .= "<optgroup label='Top Countries'>";
		foreach($top_countries as $value){
			if(array_key_exists($value, $countries)){
				if($value === $top_selection){
					$selected = "SELECTED";
				}
			$html .= "<option value='{$value}'{$selected}>{$countries[$value]}</option>";
			$selected = NULL;
			}
		}
		$html .= "</optgroup>";
	}

	if($show_all){
		$html .= "<optgroup label='All Countries'>";
		foreach($countries as $key => $country){
			if($key === $all_selection){
				$selected = "SELECTED";
			}
			$html .= "<option value='{$key}'{$selected}>{$country}</option>";
			$selected = NULL;
		}
		$html .= "</optgroup>";
	}
	
	$html .= "</select>";
	return $html;
    }

//Last edited by: Thomas Stapleton

Clone this wiki locally