php drop down box list of countries made by foreach array

/images/default.jpg

This is some code I wrote to generate a drop down box with all the countries listed in it,  that will preselect the right country if you post the country to it from a previous page. Really helpful if you use it as a control. I have it permanently in my PHP includes folder and just call it any time I need it.eg.:

 <select name=”example”>

<? include ‘../inc/countries.php’ ?>

</select>

Here is countries.php

<? 

echo ‘<option value=”">Countries…</option>

<option value=”New Zealand”>New Zealand</option>

<option value=”United Kingdom”>United Kingdom</option>

<option value=”United States of America”>United States of America</option>

<option value=”Australia”>Australia</option>

echo “<option value=”">_____________</option>’;

$country_list = array(

“Afghanistan”,

“Albania”,

“Algeria”,

“Andorra”,

“Angola”,

“Antigua and Barbuda”,

“Argentina”,

“Armenia”,

“Australia”,

“Austria”,

“Azerbaijan”,

“Bahamas”,

“Bahrain”,

“Bangladesh”,

“Barbados”,

“Belarus”,

“Belgium”,

“Belize”,

“Benin”,

“Bhutan”,

“Bolivia”,

“Bosnia and Herzegovina”,

“Botswana”,

“Brazil”,

“Brunei”,

“Bulgaria”,

“Burkina Faso”,

“Burundi”,

“Cambodia”,

“Cameroon”,

“Canada”,

“Cape Verde”,

“Central African Republic”,

“Chad”,

“Chile”,

“China”,

“Colombi”,

“Comoros”,

“Congo (Brazzaville)”,

“Congo”,

“Costa Rica”,

“Cote d’Ivoire”,

“Croatia”,

“Cuba”,

“Cyprus”,

“Czech Republic”,

“Denmark”,

“Djibouti”,

“Dominica”,

“Dominican Republic”,

“East Timor (Timor Timur)”,

“Ecuador”,

“Egypt”,

“El Salvador”,

“Equatorial Guinea”,

“Eritrea”,

“Estonia”,

“Ethiopia”,

“Fiji”,

“Finland”,

“France”,

“Gabon”,

“Gambia, The”,

“Georgia”,

“Germany”,

“Ghana”,

“Greece”,

“Grenada”,

“Guatemala”,

“Guinea”,

“Guinea-Bissau”,

“Guyana”,

“Haiti”,

“Honduras”,

“Hungary”,

“Iceland”,

“India”,

“Indonesia”,

“Iran”,

“Iraq”,

“Ireland”,

“Israel”,

“Italy”,

“Jamaica”,

“Japan”,

“Jordan”,

“Kazakhstan”,

“Kenya”,

“Kiribati”,

“Korea, North”,

“Korea, South”,

“Kuwait”,

“Kyrgyzstan”,

“Laos”,

“Latvia”,

“Lebanon”,

“Lesotho”,

“Liberia”,

“Libya”,

“Liechtenstein”,

“Lithuania”,

“Luxembourg”,

“Macedonia”,

“Madagascar”,

“Malawi”,

“Malaysia”,

“Maldives”,

“Mali”,

“Malta”,

“Marshall Islands”,

“Mauritania”,

“Mauritius”,

“Mexico”,

“Micronesia”,

“Moldova”,

“Monaco”,

“Mongolia”,

“Morocco”,

“Mozambique”,

“Myanmar”,

“Namibia”,

“Nauru”,

“Nepa”,

“Netherlands”,

“New Zealand”,

“Nicaragua”,

“Niger”,

“Nigeria”,

“Norway”,

“Oman”,

“Pakistan”,

“Palau”,

“Panama”,

“Papua New Guinea”,

“Paraguay”,

“Peru”,

“Philippines”,

“Poland”,

“Portugal”,

“Qatar”,

“Romania”,

“Russia”,

“Rwanda”,

“Saint Kitts and Nevis”,

“Saint Lucia”,

“Saint Vincent”,

“Samoa”,

“San Marino”,

“Sao Tome and Principe”,

“Saudi Arabia”,

“Senegal”,

“Serbia and Montenegro”,

“Seychelles”,

“Sierra Leone”,

“Singapore”,

“Slovakia”,

“Slovenia”,

“Solomon Islands”,

“Somalia”,

“South Africa”,

“Spain”,

“Sri Lanka”,

“Sudan”,

“Suriname”,

“Swaziland”,

“Sweden”,

“Switzerland”,

“Syria”,

“Taiwan”,

“Tajikistan”,

“Tanzania”,

“Thailand”,

“Togo”,

“Tonga”,

“Trinidad and Tobago”,

“Tunisia”,

“Turkey”,

“Turkmenistan”,

“Tuvalu”,

“Uganda”,

“Ukraine”,

“United Arab Emirates”,

“United Kingdom”,

“United States”,

“Uruguay”,

“Uzbekistan”,

“Vanuatu”,

“Vatican City”,

“Venezuela”,

“Vietnam”,

“Yemen”,

“Zambia”,

“Zimbabwe”

);

foreach ($country_list as $country) {

echo “<option value=’$country’”;

if (isset($_POST[’country’]) && $_POST[’country’]==$country) {

echo ” selected=’selected’”;

echo “>$country</option>”;

}

?>

Tags: , , , , , ,

2 Responses to “php drop down box list of countries made by foreach array”

  1. rye Says:

    This helped. Thanks

  2. Taneka Fetui Says:

    Heya Im attempting to register for your blog feed ,, but its not functioning . What is going wrong ??

Leave a Reply