» Birthday Bundle - Over $400 worth of Envato files for just $20

HTML/PHP Dynamic DropDown with countries

Posted on November 8, 2008, Filled under PHP,  Bookmark it

This is a snippet that creates a dynamic dropdown menu with countries. The select is created from the $countries array. You can set a country that will be selected automatically when the drop down is loaded. This can be done by editing the variable $default. You can specify the key from the array (i.e.: 220 for US) or the country name (i.e.: Argentina).

Dynamic DropDown (PHP)

config.php

<?php
/*
---------------
Countries List
---------------
*/

$countries = array('1' => 'Afghanistan',
'2' => 'Albania',
'3' => 'Algeria',
'4' => 'American Samoa',
'5' => 'Andorra',
'6' => 'Angola',
'7' => 'Anguilla',
'8' => 'Antarctica',
'9' => 'Antigua and Barbuda',
'10' => 'Argentina',
'11' => 'Armenia',
'12' => 'Aruba',
'13' => 'Australia',
'14' => 'Austria',
'15' => 'Azerbaidjan',
'16' => 'Bahamas',
'17' => 'Bahrain',
'18' => 'Bangladesh',
'19' => 'Barbados',
'20' => 'Belarus',
'21' => 'Belgium',
'22' => 'Belize',
'23' => 'Benin',
'24' => 'Bermuda',
'25' => 'Bhutan',
'26' => 'Bolivia',
'27' => 'Bosnia-Herzegovina',
'28' => 'Botswana',
'29' => 'Bouvet Island',
'30' => 'Brazil',
'31' => 'British Indian Ocean Territory',
'32' => 'Brunei Darussalam',
'33' => 'Bulgaria',
'34' => 'Burkina Faso',
'35' => 'Burundi',
'36' => 'Cambodia',
'37' => 'Cameroon',
'38' => 'Canada',
'39' => 'Cape Verde',
'40' => 'Cayman Islands',
'41' => 'Central African Republic',
'42' => 'Chad',
'43' => 'Chile',
'44' => 'China',
'45' => 'Christmas Island',
'46' => 'Cocos (Keeling) Islands',
'47' => 'Colombia',
'48' => 'Comoros',
'49' => 'Congo',
'50' => 'Cook Islands',
'51' => 'Costa Rica',
'52' => 'Croatia',
'53' => 'Cyprus',
'54' => 'Czech Republic',
'55' => 'Denmark',
'56' => 'Djibouti',
'57' => 'Dominica',
'58' => 'Dominican Republic',
'59' => 'East Timor',
'60' => 'Ecuador',
'61' => 'Egypt',
'62' => 'El Salvador',
'63' => 'Equatorial Guinea',
'64' => 'Eritrea',
'65' => 'Estonia',
'66' => 'Ethiopia',
'67' => 'Falkland Islands',
'68' => 'Faroe Islands',
'69' => 'Fiji',
'70' => 'Finland',
'71' => 'Former USSR',
'72' => 'France',
'73' => 'France (European Territory)',
'74' => 'French Guyana',
'75' => 'French Southern Territories',
'76' => 'Gabon',
'77' => 'Gambia',
'78' => 'Georgia',
'79' => 'Germany',
'80' => 'Ghana',
'81' => 'Gibraltar',
'82' => 'Greece',
'83' => 'Greenland',
'84' => 'Grenada',
'85' => 'Guadeloupe (French)',
'86' => 'Guam',
'87' => 'Guatemala',
'88' => 'Guinea',
'89' => 'Guinea Bissau',
'90' => 'Guyana',
'91' => 'Haiti',
'92' => 'Heard and McDonald Islands',
'93' => 'Honduras',
'94' => 'Hong Kong',
'95' => 'Hungary',
'96' => 'Iceland',
'97' => 'India',
'98' => 'Indonesia',
'99' => 'Iraq',
'100' => 'Ireland',
'101' => 'Israel',
'102' => 'Italy',
'103' => 'Ivory Coast',
'104' => 'Jamaica',
'105' => 'Japan',
'106' => 'Jordan',
'107' => 'Kazakhstan',
'108' => 'Kenya',
'109' => 'Kiribati',
'110' => 'Kuwait',
'111' => 'Kyrgyzstan',
'112' => 'Laos',
'113' => 'Latvia',
'114' => 'Lebanon',
'115' => 'Lesotho',
'116' => 'Liberia',
'117' => 'Libya',
'118' => 'Liechtenstein',
'119' => 'Lithuania',
'120' => 'Luxembourg',
'121' => 'Macau',
'122' => 'Macedonia',
'123' => 'Madagascar',
'124' => 'Malawi',
'125' => 'Malaysia',
'126' => 'Maldives',
'127' => 'Mali',
'128' => 'Malta',
'129' => 'Marshall Islands',
'130' => 'Martinique (French)',
'131' => 'Mauritania',
'132' => 'Mauritius',
'133' => 'Mayotte',
'134' => 'Mexico',
'135' => 'Micronesia',
'136' => 'Moldavia',
'137' => 'Monaco',
'138' => 'Mongolia',
'139' => 'Montserrat',
'140' => 'Morocco',
'141' => 'Mozambique',
'142' => 'Myanmar, Union of (Burma)',
'143' => 'Namibia',
'144' => 'Nauru',
'145' => 'Nepal',
'146' => 'Netherlands',
'147' => 'Netherlands Antilles',
'148' => 'Neutral Zone',
'149' => 'New Caledonia (French)',
'150' => 'New Zealand',
'151' => 'Nicaragua',
'152' => 'Niger',
'153' => 'Nigeria',
'154' => 'Niue',
'155' => 'Norfolk Island',
'156' => 'Northern Mariana Islands',
'157' => 'Norway',
'158' => 'Oman',
'159' => 'Pakistan',
'160' => 'Palau',
'161' => 'Panama',
'162' => 'Papua New Guinea',
'163' => 'Paraguay',

'164' => 'Peru',
'165' => 'Philippines',
'166' => 'Pitcairn Island',
'167' => 'Poland',
'168' => 'Polynesia (French)',
'169' => 'Portugal',
'170' => 'Qatar',
'171' => 'Reunion (French)',
'172' => 'Romania',
'173' => 'Russian Federation',
'174' => 'Rwanda',
'175' => 'S. Georgia & S. Sandwich Islands',
'176' => 'Saint Helena',
'177' => 'Saint Kitts & Nevis Anguilla',
'178' => 'Saint Lucia',
'179' => 'Saint Pierre and Miquelon',
'180' => 'Saint Tome and Principe',
'181' => 'Saint Vincent & Grenadines',
'182' => 'Samoa',
'183' => 'San Marino',
'184' => 'Saudi Arabia',
'185' => 'Senegal',
'186' => 'Seychelles',
'187' => 'Sierra Leone',
'188' => 'Singapore',
'189' => 'Slovakia',
'190' => 'Slovenia',
'191' => 'Solomon Islands',
'192' => 'Somalia',
'193' => 'South Africa',
'194' => 'South Korea',
'195' => 'Spain',
'196' => 'Sri Lanka',
'197' => 'Suriname',
'198' => 'Svalbard and Jan Mayen Islands',
'199' => 'Swaziland',
'200' => 'Sweden',
'201' => 'Switzerland',
'202' => 'Tadjikistan',
'203' => 'Taiwan',
'204' => 'Tanzania',
'205' => 'Thailand',
'206' => 'Togo',
'207' => 'Tokelau',
'208' => 'Tonga',
'209' => 'Trinidad and Tobago',
'210' => 'Tunisia',
'211' => 'Turkey',
'212' => 'Turkmenistan',
'213' => 'Turks and Caicos Islands',
'214' => 'Tuvalu',
'215' => 'Uganda',
'216' => 'UK',
'217' => 'Ukraine',
'218' => 'United Arab Emirates',
'219' => 'Uruguay',
'220' => 'US',
'221' => 'USA Minor Outlying Islands',
'222' => 'Uzbekistan',
'223' => 'Vanuatu',
'224' => 'Vatican City',
'225' => 'Venezuela',
'226' => 'Vietnam',
'227' => 'Virgin Islands (British)',
'228' => 'Virgin Islands (USA)',
'229' => 'Wallis and Futuna Islands',
'230' => 'Western Sahara',
'231' => 'Yemen',
'232' => 'Yugoslavia',
'233' => 'Zaire',
'234' => 'Zambia',
'235' => 'Zimbabwe');

/*
Searches the array for a given value (insensitive case) and returns the corresponding key if successful
*/

function array_isearch($value, $array)
{
   while (list($key, $val) = each($array))
   {
      $val = strtolower($val);
	  $value = strtolower($value);

      if($val == $value) return $key;
   }
   return false;
}
?>

Read the rest of this entry

Creating the array_isearch() Function

Posted on November 8, 2008, Filled under PHP,  Bookmark it

Hello coders,

We know that arrray_search() is a function used to search the array for given value and return the corresponding key. This works fine in a case sensitive mode, but in an insensitive one we have a problem. If we search for ‘blue’ and there is a value in array ‘BLUE’, our array_search() will return NULL. Here’s a function that works like array_search() & ignores the sensitive case:

<?php
function array_isearch($value, $array)
{
   while (list($key, $val) = each($array))
   {
      $val = strtolower($val);
	  $value = strtolower($value);

      if($val == $value) return $key;
   }
return false;
}
?>

Usage Example:

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key_one = array_isearch('BLUE', $array);

echo 'Key One: '.$key_one;

echo '<br />';

$key_two = array_isearch('GrEeN', $array);

echo 'Key Two: '.$key_two;
?>