Helper dropdown country code ( Codeigniter )

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: o – the country code array (which you can easily use for your own applications) and o – 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 dd37bbbb63a10ed4a19bdc7b5b9efbcd25f4789f1#39;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) Top countries to show in list (array)  3) Selected country (eg. ‘Australia’)  4) Show all countries (TRUE = show all, FALSE = only show top countries)

Example usage:

echo country_dropdown('country', array('US','CA','GB','DE','BR','IT','ES','AU','NZ','HK'));
// Returns a list of ALL countries, with the listed 10 shown at the top of the list

And the helper code is thus:

function  country_dropdown ( $name="country", $top_countries=array(), $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}'>";
    $selected = NULL;
    if(in_array($selection,$top_countries))  {
        $top_selection = $selection;
        $all_selection = NULL;
        }
    else  {
        $top_selection = NULL;
        $all_selection = $selection;
        }

    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;
    }
Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: