Form -- country code generated from an array of countries + codes


/ Published in: PHP
Save to your folder(s)

After i saw this snippet http://snipplr.com/view/33790/form-country-code-select-with-if-selected-test/ i wanted to make the code better :D
To make it easier to maintain


Copy this code and paste it in your HTML
  1. <?php
  2. $countries = array(
  3. "AU" => "Australia",
  4. "AF" => "Afghanistan",
  5. "AL" => "Albania",
  6. "DZ" => "Algeria",
  7. "AS" => "American Samoa",
  8. "AD" => "Andorra",
  9. "AO" => "Angola",
  10. "AI" => "Anguilla",
  11. "AQ" => "Antarctica",
  12. "AG" => "Antigua & Barbuda",
  13. "AR" => "Argentina",
  14. "AM" => "Armenia",
  15. "AW" => "Aruba",
  16. "AT" => "Austria",
  17. "AZ" => "Azerbaijan",
  18. "BS" => "Bahamas",
  19. "BH" => "Bahrain",
  20. "BD" => "Bangladesh",
  21. "BB" => "Barbados",
  22. "BY" => "Belarus",
  23. "BE" => "Belgium",
  24. "BZ" => "Belize",
  25. "BJ" => "Benin",
  26. "BM" => "Bermuda",
  27. "BT" => "Bhutan",
  28. "BO" => "Bolivia",
  29. "BA" => "Bosnia/Hercegovina",
  30. "BW" => "Botswana",
  31. "BV" => "Bouvet Island",
  32. "BR" => "Brazil",
  33. "IO" => "British Indian Ocean Territory",
  34. "BN" => "Brunei Darussalam",
  35. "BG" => "Bulgaria",
  36. "BF" => "Burkina Faso",
  37. "BI" => "Burundi",
  38. "KH" => "Cambodia",
  39. "CM" => "Cameroon",
  40. "CA" => "Canada",
  41. "CV" => "Cape Verde",
  42. "KY" => "Cayman Is",
  43. "CF" => "Central African Republic",
  44. "TD" => "Chad",
  45. "CL" => "Chile",
  46. "CN" => "China, People's Republic of",
  47. "CX" => "Christmas Island",
  48. "CC" => "Cocos Islands",
  49. "CO" => "Colombia",
  50. "KM" => "Comoros",
  51. "CG" => "Congo",
  52. "CD" => "Congo, Democratic Republic",
  53. "CK" => "Cook Islands",
  54. "CR" => "Costa Rica",
  55. "CI" => "Cote d'Ivoire",
  56. "HR" => "Croatia",
  57. "CU" => "Cuba",
  58. "CY" => "Cyprus",
  59. "CZ" => "Czech Republic",
  60. "DK" => "Denmark",
  61. "DJ" => "Djibouti",
  62. "DM" => "Dominica",
  63. "DO" => "Dominican Republic",
  64. "TP" => "East Timor",
  65. "EC" => "Ecuador",
  66. "EG" => "Egypt",
  67. "SV" => "El Salvador",
  68. "GQ" => "Equatorial Guinea",
  69. "ER" => "Eritrea",
  70. "EE" => "Estonia",
  71. "ET" => "Ethiopia",
  72. "FK" => "Falkland Islands",
  73. "FO" => "Faroe Islands",
  74. "FJ" => "Fiji",
  75. "FI" => "Finland",
  76. "FR" => "France",
  77. "FX" => "France, Metropolitan",
  78. "GF" => "French Guiana",
  79. "PF" => "French Polynesia",
  80. "TF" => "French South Territories",
  81. "GA" => "Gabon",
  82. "GM" => "Gambia",
  83. "GE" => "Georgia",
  84. "DE" => "Germany",
  85. "GH" => "Ghana",
  86. "GI" => "Gibraltar",
  87. "GR" => "Greece",
  88. "GL" => "Greenland",
  89. "GD" => "Grenada",
  90. "GP" => "Guadeloupe",
  91. "GU" => "Guam",
  92. "GT" => "Guatemala",
  93. "GN" => "Guinea",
  94. "GW" => "Guinea-Bissau",
  95. "GY" => "Guyana",
  96. "HT" => "Haiti",
  97. "HM" => "Heard Island And Mcdonald Island",
  98. "HN" => "Honduras",
  99. "HK" => "Hong Kong",
  100. "HU" => "Hungary",
  101. "IS" => "Iceland",
  102. "IN" => "India",
  103. "ID" => "Indonesia",
  104. "IR" => "Iran",
  105. "IQ" => "Iraq",
  106. "IE" => "Ireland",
  107. "IL" => "Israel",
  108. "IT" => "Italy",
  109. "JM" => "Jamaica",
  110. "JP" => "Japan",
  111. "JT" => "Johnston Island",
  112. "JO" => "Jordan",
  113. "KZ" => "Kazakhstan",
  114. "KE" => "Kenya",
  115. "KI" => "Kiribati",
  116. "KP" => "Korea, Democratic Peoples Republic",
  117. "KR" => "Korea, Republic of",
  118. "KW" => "Kuwait",
  119. "KG" => "Kyrgyzstan",
  120. "LA" => "Lao People's Democratic Republic",
  121. "LV" => "Latvia",
  122. "LB" => "Lebanon",
  123. "LS" => "Lesotho",
  124. "LR" => "Liberia",
  125. "LY" => "Libyan Arab Jamahiriya",
  126. "LI" => "Liechtenstein",
  127. "LT" => "Lithuania",
  128. "LU" => "Luxembourg",
  129. "MO" => "Macau",
  130. "MK" => "Macedonia",
  131. "MG" => "Madagascar",
  132. "MW" => "Malawi",
  133. "MY" => "Malaysia",
  134. "MV" => "Maldives",
  135. "ML" => "Mali",
  136. "MT" => "Malta",
  137. "MH" => "Marshall Islands",
  138. "MQ" => "Martinique",
  139. "MR" => "Mauritania",
  140. "MU" => "Mauritius",
  141. "YT" => "Mayotte",
  142. "MX" => "Mexico",
  143. "FM" => "Micronesia",
  144. "MD" => "Moldavia",
  145. "MC" => "Monaco",
  146. "MN" => "Mongolia",
  147. "MS" => "Montserrat",
  148. "MA" => "Morocco",
  149. "MZ" => "Mozambique",
  150. "MM" => "Union Of Myanmar",
  151. "NA" => "Namibia",
  152. "NR" => "Nauru Island",
  153. "NP" => "Nepal",
  154. "NL" => "Netherlands",
  155. "AN" => "Netherlands Antilles",
  156. "NC" => "New Caledonia",
  157. "NZ" => "New Zealand",
  158. "NI" => "Nicaragua",
  159. "NE" => "Niger",
  160. "NG" => "Nigeria",
  161. "NU" => "Niue",
  162. "NF" => "Norfolk Island",
  163. "MP" => "Mariana Islands, Northern",
  164. "NO" => "Norway",
  165. "OM" => "Oman",
  166. "PK" => "Pakistan",
  167. "PW" => "Palau Islands",
  168. "PS" => "Palestine",
  169. "PA" => "Panama",
  170. "PG" => "Papua New Guinea",
  171. "PY" => "Paraguay",
  172. "PE" => "Peru",
  173. "PH" => "Philippines",
  174. "PN" => "Pitcairn",
  175. "PL" => "Poland",
  176. "PT" => "Portugal",
  177. "PR" => "Puerto Rico",
  178. "QA" => "Qatar",
  179. "RE" => "Reunion Island",
  180. "RO" => "Romania",
  181. "RU" => "Russian Federation",
  182. "RW" => "Rwanda",
  183. "WS" => "Samoa",
  184. "SH" => "St Helena",
  185. "KN" => "St Kitts & Nevis",
  186. "LC" => "St Lucia",
  187. "PM" => "St Pierre & Miquelon",
  188. "VC" => "St Vincent",
  189. "SM" => "San Marino",
  190. "ST" => "Sao Tome & Principe",
  191. "SA" => "Saudi Arabia",
  192. "SN" => "Senegal",
  193. "SC" => "Seychelles",
  194. "SL" => "Sierra Leone",
  195. "SG" => "Singapore",
  196. "SK" => "Slovakia",
  197. "SI" => "Slovenia",
  198. "SB" => "Solomon Islands",
  199. "SO" => "Somalia",
  200. "ZA" => "South Africa",
  201. "GS" => "South Georgia and South Sandwich",
  202. "ES" => "Spain",
  203. "LK" => "Sri Lanka",
  204. "XX" => "Stateless Persons",
  205. "SD" => "Sudan",
  206. "SR" => "Suriname",
  207. "SJ" => "Svalbard and Jan Mayen",
  208. "SZ" => "Swaziland",
  209. "SE" => "Sweden",
  210. "CH" => "Switzerland",
  211. "SY" => "Syrian Arab Republic",
  212. "TW" => "Taiwan, Republic of China",
  213. "TJ" => "Tajikistan",
  214. "TZ" => "Tanzania",
  215. "TH" => "Thailand",
  216. "TL" => "Timor Leste",
  217. "TG" => "Togo",
  218. "TK" => "Tokelau",
  219. "TO" => "Tonga",
  220. "TT" => "Trinidad & Tobago",
  221. "TN" => "Tunisia",
  222. "TR" => "Turkey",
  223. "TM" => "Turkmenistan",
  224. "TC" => "Turks And Caicos Islands",
  225. "TV" => "Tuvalu",
  226. "UG" => "Uganda",
  227. "UA" => "Ukraine",
  228. "AE" => "United Arab Emirates",
  229. "GB" => "United Kingdom",
  230. "UM" => "US Minor Outlying Islands",
  231. "US" => "USA",
  232. "HV" => "Upper Volta",
  233. "UY" => "Uruguay",
  234. "UZ" => "Uzbekistan",
  235. "VU" => "Vanuatu",
  236. "VA" => "Vatican City State",
  237. "VE" => "Venezuela",
  238. "VN" => "Vietnam",
  239. "VG" => "Virgin Islands (British)",
  240. "VI" => "Virgin Islands (US)",
  241. "WF" => "Wallis And Futuna Islands",
  242. "EH" => "Western Sahara",
  243. "YE" => "Yemen Arab Rep.",
  244. "YD" => "Yemen Democratic",
  245. "YU" => "Yugoslavia",
  246. "ZR" => "Zaire",
  247. "ZM" => "Zambia",
  248. "ZW" => "Zimbabwe"
  249. );
  250.  
  251. $selected = ' selected="selected"';
  252. $_country = NULL;
  253. if ( isset($_GET['country']) ) {
  254. $_country = isset($countries[$_GET['country']]) ? $_GET['country'] : NULL;
  255. }
  256.  
  257. echo "<select name=\"country\">";
  258. echo "<option>--- Please select your country --- </option>\n";
  259. foreach ( $countries as $code => $country ) {
  260. echo "<option value=\"$code\"";
  261. if ( $_country == $code ) {
  262. echo $selected;
  263. }
  264. echo ">$country</option>\n";
  265. }
  266. echo "</select>";
  267. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.