An ISO standard country name drop down list for HTML form
Hello Guys, recently i got a small post in my codeigniter wiki page(http://goo.gl/nEbsJ) about a ISO standard country code. Actually, before this i'd not any idea about such type of thing. In-fact i can remember in my first "create account" form i'd manually made a list of countries by coping them from an other site. But, right now i'm smiling by remembering all those non-standard work which i'd done once upon a time.



Any way, the parent link which i has shown in top is for codeigniter. So, i think why not i modify that so that my plain PHP programmer friends can use that in their own work. So, here is the code i'm going to share.. which i've just modify a bit.




<?php
$countryList 
= 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"); 

function 
dropDownCountryList($selectName,$countryList,$topCountry){
  if(
$selectName == '')
  
$selectName 'country';
 
  echo 
'<select name="'.$selectName.'">'
  if (
count($topCountry))
  { 
   foreach(
$topCountry as $item): 
    foreach(
$countryList as $countryListKey => $countryListValue):
     if(
$item == $countryListKey)
     echo 
'<option value="'.$countryListKey.'">'.$countryListValue.'</option>';
    endforeach;
   endforeach;
  }
    
  foreach(
$countryList as $k => $v):
  echo 
'<option value="'.$k.'">'.$v.'</option>';
  endforeach;
  echo 
'</select>';
}


$topCountry = array('US','CA','GB','DE','BR','IT','ES','AU','NZ','HK');
$selectName 'countryList';
echo 
dropDownCountryList($selectName,$countryList,$topCountry); 





It has just 2 parts.

* Country list array which hold all the countries name along with it's 2 char country codes.

* A function "dropDownCountryList()" which will do the work for you.



The "dropDownCountryList()" has only 3 parameters. These are...

[1] $selectName : It's just the name of the SELECT form. By default it's value is "country". If you'll not provide any select name this default value will get used.



[2] $countryList : This is the country array which hold all the countries name along with it's 2 char country codes.



[3] $topCountry : This is a parameter which hold some of the country code which you might want to show in the top of all country list for quick pick. This is an optional parameter. SO, if you'll not used then he default country list will appear.



Optional parameter: $selectName, $topCountry

Required parameter : $countryList



Download the original file from here : http://www.box.net/shared/k4oo6xfzny



So Guys, that's it. I hope, you'll like it & use it in your work. If this, if any body will get any problem you can post your query at here.



Ref: http://goo.gl/nEbsJ

16th Feb 2011 at 2:47 PM   3641 Views

Oop's, No Comments has been droped for this thread yet now.
Why don't you drop one.