Its quiet a tedious job when it comes to developing a dropdown input field in which the user have to select a county, as part of their address, Nationality, etc. Think if you have several of this fields in your form. When you want to change you have to change each and every one. More over, if you have to select one option depending upon user input or some database value, your will either have to have ‘if’ statement in every ‘<option>’ tag or use javascript to select the option when the page loads. As I am a lazy guy, decided to have an array containing the names of the countries as values and the 2 digit alphabetic country codes as key. Now you can see how easily you can add change and manipulate these select field.
[PHP]
echo "Hello World";
?>
[/PHP]
[CSS]
*{
margin:0px;
padding:0px;
border:0px;
}
[/CSS]
PHP key-value array
[PHP]
$Nation['ad']= ‘Andorra’;
$Nation['ae']= ‘United Arab Emirates’;
$Nation['af'] = ‘Afghanistan’;
$Nation['ag'] = ‘Antigua and Barbuda’;
$Nation['ai'] = ‘Anguilla’;
$Nation['al'] = ‘Albania’;
$Nation['am'] = ‘Armenia’;
$Nation['an'] = ‘Netherlands Antilles’;
$Nation['ao'] = ‘Angola’;
$Nation['aq'] = ‘Antarctica’;
$Nation['ar'] = ‘Argentina’;
$Nation['as'] = ‘American Samoa’;
$Nation['at'] = ‘Austria’;
$Nation['au'] = ‘Australia’;
$Nation['aw'] = ‘Aruba’;
$Nation['az'] = ‘Azerbaijan’;
$Nation['ba'] = ‘Bosnia and Herzegovina’;
$Nation['bb'] = ‘Barbados’;
$Nation['bd'] = ‘Bangladesh’;
$Nation['be'] = ‘Belgium’;
$Nation['bf'] = ‘Burkina Faso’;
$Nation['bg'] = ‘Bulgaria’;
$Nation['bh'] = ‘Bahrain’;
$Nation['bi'] = ‘Burundi’;
$Nation['bj'] = ‘Benin’;
$Nation['bm'] = ‘Bermuda’;
$Nation['bn'] = ‘Brunei Darussalam’;
$Nation['bo'] = ‘Bolivia’;
$Nation['br'] = ‘Brazil’;
$Nation['bs'] = ‘Bahamas’;
$Nation['bt'] = ‘Bhutan’;
$Nation['bv'] = ‘Bouvet Island’;
$Nation['bw'] = ‘Botswana’;
$Nation['by'] = ‘Belarus’;
$Nation['bz'] = ‘Beliza’;
$Nation['ca'] = ‘Canada’;
$Nation['cc'] = ‘Cocos (Keeling) Islands’;
$Nation['cf'] = ‘Central African Republic’;
$Nation['cg'] = ‘Congo’;
$Nation['ch'] = ‘Switzerland’;
$Nation['ci'] = ‘Cote DIvoire (Ivory Coast)’;
$Nation['ck'] = ‘Cook Islands’;
$Nation['cl'] = ‘Chile’;
$Nation['cm'] = ‘Cameroon’;
$Nation['cn'] = ‘China’;
$Nation['co'] = ‘Colombia’;
$Nation['cr'] = ‘Costa Rica’;
$Nation['cs'] = ‘Czechoslovakia (former)’;
$Nation['cu'] = ‘Cuba’;
$Nation['cv'] = ‘Cape Verde’;
$Nation['cx'] = ‘Christmas Island’;
$Nation['cy'] = ‘Cyprus’;
$Nation['cz'] = ‘Czech Republic’;
$Nation['de'] = ‘Germany’;
$Nation['dj'] = ‘Djibouti’;
$Nation['dk'] = ‘Denmark’;
$Nation['dm'] = ‘Dominica’;
$Nation['do'] = ‘Dominican Republic’;
$Nation['dz'] = ‘Algeria’;
$Nation['ec'] = ‘Ecuador’;
$Nation['ee'] = ‘Estonia’;
$Nation['eg'] = ‘Egypt’;
$Nation['eh'] = ‘Western Samoa’;
$Nation['er'] = ‘Eritrea’;
$Nation['es'] = ‘Spain’;
$Nation['et'] = ‘Ethiopia’;
$Nation['fi'] = ‘Finland’;
$Nation['fj'] = ‘Fiji’;
$Nation['fk'] = ‘Falkland Islands (Malvinas)’;
$Nation['fm'] = ‘Micronesia’;
$Nation['fo'] = ‘Faroe Islands’;
$Nation['fr'] = ‘France’;
$Nation['fx'] = ‘France (metropolitan)’;
$Nation['ga'] = ‘Gabon’;
$Nation['gb'] = ‘Great Britain’;
$Nation['gd'] = ‘Grenada’;
$Nation['ge'] = ‘Georgia’;
$Nation['gf'] = ‘French Guiana’;
$Nation['gh'] = ‘Ghana’;
$Nation['gi'] = ‘Gibraltar’;
$Nation['gl'] = ‘Greenland’;
$Nation['gm'] = ‘Gambia’;
$Nation['gn'] = ‘Guinea’;
$Nation['gp'] = ‘Guadeloupe’;
$Nation['gq'] = ‘Equatorial Guinea’;
$Nation['gr'] = ‘Greece’;
$Nation['gs'] = ‘St. Georgia & Sandwich Islands’;
$Nation['gt'] = ‘Guatemala’;
$Nation['gu'] = ‘Guam’;
$Nation['gw'] = ‘Guinea-Bissau’;
$Nation['gy'] = ‘Guyana’;
$Nation['hk'] = ‘Hong Kong’;
$Nation['hm'] = ‘Heard and McDonald Islands’;
$Nation['hn'] = ‘Honduras’;
$Nation['hr'] = ‘Croatia (Hrvatska)’;
$Nation['ht'] = ‘Haiti’;
$Nation['hu'] = ‘Hungary’;
$Nation['id'] = ‘Indonesia’;
$Nation['ie'] = ‘Ireland’;
$Nation['il'] = ‘Israel’;
$Nation['in'] = ‘India’;
$Nation['io'] = ‘British Indian Ocean Territory’;
$Nation['iq'] = ‘Iraq’;
$Nation['ir'] = ‘Iran’;
$Nation['is'] = ‘Iceland’;
$Nation['it'] = ‘Italy’;
$Nation['jm'] = ‘Jamaica’;
$Nation['jo'] = ‘Jordan’;
$Nation['jp'] = ‘Japan’;
$Nation['ke'] = ‘Kenya’;
$Nation['kg'] = ‘Kyrgystan’;
$Nation['kh'] = ‘Cambodia’;
$Nation['ki'] = ‘Kiribati’;
$Nation['km'] = ‘Comoros’;
$Nation['kn'] = ‘Saint Kitts and Nevis’;
$Nation['kp'] = ‘Korea (North)’;
$Nation['kr'] = ‘Korea (South)’;
$Nation['kw'] = ‘Kuwait’;
$Nation['ky'] = ‘Cayman Islands’;
$Nation['kz'] = ‘Kazakhstan’;
$Nation['la'] = ‘Laos’;
$Nation['lb'] = ‘Lebanon’;
$Nation['lc'] = ‘Saint Lucia’;
$Nation['li'] = ‘Liechtenstein’;
$Nation['lk'] = ‘Sri Lanka’;
$Nation['lr'] = ‘Liberia’;
$Nation['ls'] = ‘Lesotho’;
$Nation['lt'] = ‘Lithuania’;
$Nation['lu'] = ‘Luxembourg’;
$Nation['lv'] = ‘Latvia’;
$Nation['ly'] = ‘Libya’;
$Nation['ma'] = ‘Morocco’;
$Nation['mc'] = ‘Monaco’;
$Nation['md'] = ‘Moldova’;
$Nation['mg'] = ‘Madagascar’;
$Nation['mh'] = ‘Marshall Islands’;
$Nation['mk'] = ‘Macedonia’;
$Nation['ml'] = ‘Mali’;
$Nation['mm'] = ‘Myanmar’;
$Nation['mn'] = ‘Mongolia’;
$Nation['mo'] = ‘Macau’;
$Nation['mp'] = ‘Northern Mariana Islands’;
$Nation['mq'] = ‘Martinique’;
$Nation['mr'] = ‘Mauritania’;
$Nation['ms'] = ‘Montserrat’;
$Nation['mt'] = ‘Malta’;
$Nation['mu'] = ‘Mauritius’;
$Nation['mv'] = ‘Maldives’;
$Nation['mw'] = ‘Malawi’;
$Nation['mx'] = ‘Mexico’;
$Nation['my'] = ‘Malaysia’;
$Nation['mz'] = ‘Mozambique’;
$Nation['na'] = ‘Namibia’;
$Nation['nc'] = ‘New Caledonia’;
$Nation['ne'] = ‘Niger’;
$Nation['nf'] = ‘Norfolk Island’;
$Nation['ng'] = ‘Nigeria’;
$Nation['ni'] = ‘Nicaragua’;
$Nation['nl'] = ‘Netherlands’;
$Nation['no'] = ‘Norway’;
$Nation['np'] = ‘Nepal’;
$Nation['nr'] = ‘Nauru’;
$Nation['nt'] = ‘Neutral Zone’;
$Nation['nu'] = ‘Niue’;
$Nation['nz'] = ‘New Zealand’;
$Nation['om'] = ‘Oman’;
$Nation['pa'] = ‘Panama’;
$Nation['pe'] = ‘Peru’;
$Nation['pf'] = ‘French Polynesia’;
$Nation['pg'] = ‘Papua New Guinea’;
$Nation['ph'] = ‘Phillipines’;
$Nation['pk'] = ‘Pakistan’;
$Nation['pl'] = ‘Poland’;
$Nation['pm'] = ‘St. Pierre and Miquelon’;
$Nation['pn'] = ‘Pitcairn’;
$Nation['pr'] = ‘Puerto Rico’;
$Nation['pt'] = ‘Portugal’;
$Nation['pw'] = ‘Palau’;
$Nation['py'] = ‘Paraguay’;
$Nation['qa'] = ‘Qatar’;
$Nation['re'] = ‘Reunion’;
$Nation['ro'] = ‘Romania’;
$Nation['ru'] = ‘Russian Federation’;
$Nation['rw'] = ‘Rwanda’;
$Nation['sa'] = ‘Saudia Arabia’;
$Nation['sb'] = ‘Solomon Islands’;
$Nation['sc'] = ‘Seychelles’;
$Nation['sd'] = ‘Sudan’;
$Nation['se'] = ‘Sweden’;
$Nation['sg'] = ‘Singapore’;
$Nation['sh'] = ‘St. Helena’;
$Nation['si'] = ‘Slovenia’;
$Nation['sj'] = ‘Svalbard and Jan Mayen Islands’;
$Nation['sk'] = ‘Romania’;
$Nation['sl'] = ‘Slovak Republic’;
$Nation['sm'] = ‘Sierra Leone’;
$Nation['sn'] = ‘Senegal’;
$Nation['so'] = ‘Somalia’;
$Nation['sr'] = ‘Suriname’;
$Nation['st'] = ‘Sao Tome and Principe’;
$Nation['su'] = ‘USSR (former)’;
$Nation['sv'] = ‘El Salvador’;
$Nation['sy'] = ‘Syria’;
$Nation['sz'] = ‘Swaziland’;
$Nation['tc'] = ‘Turks and Caicos Islands’;
$Nation['td'] = ‘Chad’;
$Nation['tf'] = ‘French Southern Territories’;
$Nation['tg'] = ‘Togo’;
$Nation['th'] = ‘Thailand’;
$Nation['tj'] = ‘Tajikistan’;
$Nation['tk'] = ‘Tokelau’;
$Nation['tm'] = ‘Turkmenistan’;
$Nation['tn'] = ‘Tunisia’;
$Nation['to'] = ‘Tonga’;
$Nation['tp'] = ‘East Timor’;
$Nation['tr'] = ‘Turkey’;
$Nation['tt'] = ‘Trinidad and Tobago’;
$Nation['tv'] = ‘Tuvalu’;
$Nation['tw'] = ‘Taiwan’;
$Nation['tz'] = ‘Tanzania’;
$Nation['ua'] = ‘Ukraine’;
$Nation['ug'] = ‘Uganda’;
$Nation['uk'] = ‘United Kingdom’;
$Nation['um'] = ‘US Minor Outlying Islands’;
$Nation['us'] = ‘United States’;
$Nation['uy'] = ‘Uruguay’;
$Nation['uz'] = ‘Uzbekistan’;
$Nation['va'] = ‘Vatican City State’;
$Nation['vc'] = ‘Saint Vincent and the Grenadines’;
$Nation['ve'] = ‘Venezuela’;
$Nation['vg'] = ‘Virgin Islands (British)’;
$Nation['vi'] = ‘Virgin Islands (U.S.)’;
$Nation['vn'] = ‘Viet Nam’;
$Nation['vu'] = ‘Vanuata’;
$Nation['wf'] = ‘Wallis and Futuna Islands’;
$Nation['ws'] = ‘Samoa’;
$Nation['ye'] = ‘Yemen’;
$Nation['yt'] = ‘Mayotte’;
$Nation['yu'] = ‘Yugoslavia’;
$Nation['za'] = ‘South Africa’;
$Nation['zm'] = ‘Zambia’;
$Nation['zr'] = ‘Zaire’;
$Nation['zw'] = ‘Zimbabwe’;
?>
[/PHP]
The array can be use as follows:
Several PHP array and string function can be used to manipulate this array to get your desired results. These are some useful PHP functions that you can use:
<strong>String</strong>
string strtolower ( string $str )
string strtoupper ( string $string )
string ucfirst ( string $str )
string ucwords ( string $str )
<strong>Array</strong>
bool asort ( array &$array [, int $sort_flags= SORT_REGULAR ] )
bool arsort ( array &$array [, int $sort_flags= SORT_REGULAR ] )
bool ksort ( array &$array [, int $sort_flags= SORT_REGULAR ] )
bool krsort ( array &$array [, int $sort_flags= SORT_REGULAR ] )
bool array_key_exists ( mixed $key , array $search )
bool in_array ( mixed $needle , array $haystack [, bool $strict ] )
</p>
Freedom is not worth having if it does not include the freedom to make mistakes.
—
Hi my name is Alfie. I am an IT professional specialized in opensource web development. I work as a System Analist com Web Programmer. My blog have no restriction on the topics it deals with but mainly specialized on infotainment topics. Your constant help, support and comments will be an encouragement for my efforts.