Country Select List Helper
If you are having users register for your site, especially a customer - you may want to know their country of residence. Instead of having to create a select list and manually enter each country - this handy helper does it for you all.
To use the below helper, just put this code into your view:
The first argument is the table name in your model. The second is the label you want to show on your site. The is a third options which allows you to select the currently selected country to show. If not passed, then "Please select a country" is selected. This allows you to pass in an existing users country to the field for editing forms. Finally, you can add an array for attributes such as class and id.
You'll now get a nice fully-filled list of all the countries in the world. The values are stored as the 2 letter code for each country (i.e. GB for United Kingdom, DE for Germany).
<?php e( $countryList->select('country', 'Please select your country'));?>
The first argument is the table name in your model. The second is the label you want to show on your site. The is a third options which allows you to select the currently selected country to show. If not passed, then "Please select a country" is selected. This allows you to pass in an existing users country to the field for editing forms. Finally, you can add an array for attributes such as class and id.
You'll now get a nice fully-filled list of all the countries in the world. The values are stored as the 2 letter code for each country (i.e. GB for United Kingdom, DE for Germany).
Helper Class:
<?php
/**
* Helper for outputing a country select list.
*
* Allows you to include a selec list of all countries using 1 line of code.
*
* Author: Tane Piper (digitalspaghetti@gmail.com)
* URL: http://digitalspaghetti.me.uk
*
* PHP versions 4 and 5
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
*/
class CountryListHelper extends FormHelper
{
var $helpers = array('Form');
function select($fieldname, $label, $default=" ", $attributes)
{
$list = '<div class="input">';
$list .= $this->Form->label($label);
$list .= $this->Form->select($fieldname , array(
' ' => 'Please select a country',
'--' => 'None',
'AF' => 'Afganistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
'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 Herzegowina',
'BW' => 'Botswana',
'BV' => 'Bouvet Island',
'BR' => 'Brazil',
'IO' => 'British Indian Ocean Territory',
'BN' => 'Brunei Darussalam',
'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',
'CG' => 'Congo',
'CD' => 'Congo, the Democratic Republic of the',
'CK' => 'Cook Islands',
'CR' => 'Costa Rica',
'CI' => 'Cote d\'Ivoire',
'HR' => 'Croatia (Hrvatska)',
'CU' => 'Cuba',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'DK' => 'Denmark',
'DJ' => 'Djibouti',
'DM' => 'Dominica',
'DO' => 'Dominican Republic',
'TP' => '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',
'FX' => 'France, Metropolitan',
'GF' => 'French Guiana',
'PF' => 'French Polynesia',
'TF' => 'French Southern Territories',
'GA' => 'Gabon',
'GM' => 'Gambia',
'GE' => 'Georgia',
'DE' => 'Germany',
'GH' => 'Ghana',
'GI' => 'Gibraltar',
'GR' => 'Greece',
'GL' => 'Greenland',
'GD' => 'Grenada',
'GP' => 'Guadeloupe',
'GU' => 'Guam',
'GT' => 'Guatemala',
'GN' => 'Guinea',
'GW' => 'Guinea-Bissau',
'GY' => 'Guyana',
'HT' => 'Haiti',
'HM' => 'Heard and Mc Donald Islands',
'VA' => 'Holy See (Vatican City State)',
'HN' => 'Honduras',
'HK' => 'Hong Kong',
'HU' => 'Hungary',
'IS' => 'Iceland',
'IN' => 'India',
'ID' => 'Indonesia',
'IR' => 'Iran (Islamic Republic of)',
'IQ' => 'Iraq',
'IE' => 'Ireland',
'IL' => 'Israel',
'IT' => 'Italy',
'JM' => 'Jamaica',
'JP' => 'Japan',
'JO' => 'Jordan',
'KZ' => 'Kazakhstan',
'KE' => 'Kenya',
'KI' => 'Kiribati',
'KP' => 'Korea, Democratic People\'s Republic of',
'KR' => 'Korea, Republic of',
'KW' => 'Kuwait',
'KG' => 'Kyrgyzstan',
'LA' => 'Lao People\'s Democratic Republic',
'LV' => 'Latvia',
'LB' => 'Lebanon',
'LS' => 'Lesotho',
'LR' => 'Liberia',
'LY' => 'Libyan Arab Jamahiriya',
'LI' => 'Liechtenstein',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'MO' => 'Macau',
'MK' => 'Macedonia, The Former Yugoslav 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, Federated States of',
'MD' => 'Moldova, Republic of',
'MC' => 'Monaco',
'MN' => 'Mongolia',
'MS' => 'Montserrat',
'MA' => 'Morocco',
'MZ' => 'Mozambique',
'MM' => 'Myanmar',
'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',
'MP' => 'Northern Mariana Islands',
'NO' => 'Norway',
'OM' => 'Oman',
'PK' => 'Pakistan',
'PW' => 'Palau',
'PA' => 'Panama',
'PG' => 'Papua New Guinea',
'PY' => 'Paraguay',
'PE' => 'Peru',
'PH' => 'Philippines',
'PN' => 'Pitcairn',
'PL' => 'Poland',
'PT' => 'Portugal',
'PR' => 'Puerto Rico',
'QA' => 'Qatar',
'RE' => 'Reunion',
'RO' => 'Romania',
'RU' => 'Russian Federation',
'RW' => 'Rwanda',
'KN' => 'Saint Kitts and Nevis',
'LC' => 'Saint LUCIA',
'VC' => 'Saint Vincent and the Grenadines',
'WS' => 'Samoa',
'SM' => 'San Marino',
'ST' => 'Sao Tome and Principe',
'SA' => 'Saudi Arabia',
'SN' => 'Senegal',
'SC' => 'Seychelles',
'SL' => 'Sierra Leone',
'SG' => 'Singapore',
'SK' => 'Slovakia (Slovak Republic)',
'SI' => 'Slovenia',
'SB' => 'Solomon Islands',
'SO' => 'Somalia',
'ZA' => 'South Africa',
'GS' => 'South Georgia and the South Sandwich Islands',
'ES' => 'Spain',
'LK' => 'Sri Lanka',
'SH' => 'St. Helena',
'PM' => 'St. Pierre and Miquelon',
'SD' => 'Sudan',
'SR' => 'Suriname',
'SJ' => 'Svalbard and Jan Mayen Islands',
'SZ' => 'Swaziland',
'SE' => 'Sweden',
'CH' => 'Switzerland',
'SY' => 'Syrian Arab Republic',
'TW' => 'Taiwan, Province of China',
'TJ' => 'Tajikistan',
'TZ' => 'Tanzania, United Republic of',
'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',
'VE' => 'Venezuela',
'VN' => 'Viet Nam',
'VG' => 'Virgin Islands (British)',
'VI' => 'Virgin Islands (U.S.)',
'WF' => 'Wallis and Futuna Islands',
'EH' => 'Western Sahara',
'YE' => 'Yemen',
'YU' => 'Yugoslavia',
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe'
), $default, $attributes);
$list .= '</div>';
return $this->output($list);
}
}
?>








$fieldName,
array_merge(
$options,
array(
'empty' => __(' - Select one - ', true),
'options' => array(
'AF' => __('Afganistan', true),
'AL' => __('Albania', true),
'DZ' => __('Algeria', true),
'AS' => __('American Samoa', true),
'AD' => __('Andorra', true),
'AO' => __('Angola', true),
'AI' => __('Anguilla', true),
'AQ' => __('Antarctica', true),
'AG' => __('Antigua and Barbuda', true),
'AR' => __('Argentina', true),
'AM' => __('Armenia', true),
'AW' => __('Aruba', true),
'AU' => __('Australia', true),
'AT' => __('Austria', true),
'AZ' => __('Azerbaijan', true),
'BS' => __('Bahamas', true),
'BH' => __('Bahrain', true),
'BD' => __('Bangladesh', true),
'BB' => __('Barbados', true),
'BY' => __('Belarus', true),
'BE' => __('Belgium', true),
'BZ' => __('Belize', true),
'BJ' => __('Benin', true),
'BM' => __('Bermuda', true),
'BT' => __('Bhutan', true),
'BO' => __('Bolivia', true),
'BA' => __('Bosnia and Herzegowina', true),
'BW' => __('Botswana', true),
'BV' => __('Bouvet Island', true),
'BR' => __('Brazil', true),
'IO' => __('British Indian Ocean Territory', true),
'BN' => __('Brunei Darussalam', true),
'BG' => __('Bulgaria', true),
'BF' => __('Burkina Faso', true),
'BI' => __('Burundi', true),
'KH' => __('Cambodia', true),
'CM' => __('Cameroon', true),
'CA' => __('Canada', true),
'CV' => __('Cape Verde', true),
'KY' => __('Cayman Islands', true),
'CF' => __('Central African Republic', true),
'TD' => __('Chad', true),
'CL' => __('Chile', true),
'CN' => __('China', true),
'CX' => __('Christmas Island', true),
'CC' => __('Cocos (Keeling) Islands', true),
'CO' => __('Colombia', true),
'KM' => __('Comoros', true),
'CG' => __('Congo', true),
'CD' => __('Congo, the Democratic Republic of the', true),
'CK' => __('Cook Islands', true),
'CR' => __('Costa Rica', true),
'CI' => __('Cote d\'Ivoire', true),
'HR' => __('Croatia (Hrvatska)', true),
'CU' => __('Cuba', true),
'CY' => __('Cyprus', true),
'CZ' => __('Czech Republic', true),
'DK' => __('Denmark', true),
'DJ' => __('Djibouti', true),
'DM' => __('Dominica', true),
'DO' => __('Dominican Republic', true),
'TP' => __('East Timor', true),
'EC' => __('Ecuador', true),
'EG' => __('Egypt', true),
'SV' => __('El Salvador', true),
'GQ' => __('Equatorial Guinea', true),
'ER' => __('Eritrea', true),
'EE' => __('Estonia', true),
'ET' => __('Ethiopia', true),
'FK' => __('Falkland Islands (Malvinas)', true),
'FO' => __('Faroe Islands', true),
'FJ' => __('Fiji', true),
'FI' => __('Finland', true),
'FR' => __('France', true),
'FX' => __('France, Metropolitan', true),
'GF' => __('French Guiana', true),
'PF' => __('French Polynesia', true),
'TF' => __('French Southern Territories', true),
'GA' => __('Gabon', true),
'GM' => __('Gambia', true),
'GE' => __('Georgia', true),
'DE' => __('Germany', true),
'GH' => __('Ghana', true),
'GI' => __('Gibraltar', true),
'GR' => __('Greece', true),
'GL' => __('Greenland', true),
'GD' => __('Grenada', true),
'GP' => __('Guadeloupe', true),
'GU' => __('Guam', true),
'GT' => __('Guatemala', true),
'GN' => __('Guinea', true),
'GW' => __('Guinea-Bissau', true),
'GY' => __('Guyana', true),
'HT' => __('Haiti', true),
'HM' => __('Heard and Mc Donald Islands', true),
'VA' => __('Holy See (Vatican City State)', true),
'HN' => __('Honduras', true),
'HK' => __('Hong Kong', true),
'HU' => __('Hungary', true),
'IS' => __('Iceland', true),
'IN' => __('India', true),
'ID' => __('Indonesia', true),
'IR' => __('Iran (Islamic Republic of)', true),
'IQ' => __('Iraq', true),
'IE' => __('Ireland', true),
'IL' => __('Israel', true),
'IT' => __('Italy', true),
'JM' => __('Jamaica', true),
'JP' => __('Japan', true),
'JO' => __('Jordan', true),
'KZ' => __('Kazakhstan', true),
'KE' => __('Kenya', true),
'KI' => __('Kiribati', true),
'KP' => __('Korea, Democratic People\'s Republic of', true),
'KR' => __('Korea, Republic of', true),
'KW' => __('Kuwait', true),
'KG' => __('Kyrgyzstan', true),
'LA' => __('Lao People\'s Democratic Republic', true),
'LV' => __('Latvia', true),
'LB' => __('Lebanon', true),
'LS' => __('Lesotho', true),
'LR' => __('Liberia', true),
'LY' => __('Libyan Arab Jamahiriya', true),
'LI' => __('Liechtenstein', true),
'LT' => __('Lithuania', true),
'LU' => __('Luxembourg', true),
'MO' => __('Macau', true),
'MK' => __('Macedonia, The Former Yugoslav Republic of', true),
'MG' => __('Madagascar', true),
'MW' => __('Malawi', true),
'MY' => __('Malaysia', true),
'MV' => __('Maldives', true),
'ML' => __('Mali', true),
'MT' => __('Malta', true),
'MH' => __('Marshall Islands', true),
'MQ' => __('Martinique', true),
'MR' => __('Mauritania', true),
'MU' => __('Mauritius', true),
'YT' => __('Mayotte', true),
'MX' => __('Mexico', true),
'FM' => __('Micronesia, Federated States of', true),
'MD' => __('Moldova, Republic of', true),
'MC' => __('Monaco', true),
'MN' => __('Mongolia', true),
'MS' => __('Montserrat', true),
'MA' => __('Morocco', true),
'MZ' => __('Mozambique', true),
'MM' => __('Myanmar', true),
'NA' => __('Namibia', true),
'NR' => __('Nauru', true),
'NP' => __('Nepal', true),
'NL' => __('Netherlands', true),
'AN' => __('Netherlands Antilles', true),
'NC' => __('New Caledonia', true),
'NZ' => __('New Zealand', true),
'NI' => __('Nicaragua', true),
'NE' => __('Niger', true),
'NG' => __('Nigeria', true),
'NU' => __('Niue', true),
'NF' => __('Norfolk Island', true),
'MP' => __('Northern Mariana Islands', true),
'NO' => __('Norway', true),
'OM' => __('Oman', true),
'PK' => __('Pakistan', true),
'PW' => __('Palau', true),
'PA' => __('Panama', true),
'PG' => __('Papua New Guinea', true),
'PY' => __('Paraguay', true),
'PE' => __('Peru', true),
'PH' => __('Philippines', true),
'PN' => __('Pitcairn', true),
'PL' => __('Poland', true),
'PT' => __('Portugal', true),
'PR' => __('Puerto Rico', true),
'QA' => __('Qatar', true),
'RE' => __('Reunion', true),
'RO' => __('Romania', true),
'RU' => __('Russian Federation', true),
'RW' => __('Rwanda', true),
'KN' => __('Saint Kitts and Nevis', true),
'LC' => __('Saint LUCIA', true),
'VC' => __('Saint Vincent and the Grenadines', true),
'WS' => __('Samoa', true),
'SM' => __('San Marino', true),
'ST' => __('Sao Tome and Principe', true),
'SA' => __('Saudi Arabia', true),
'SN' => __('Senegal', true),
'SC' => __('Seychelles', true),
'SL' => __('Sierra Leone', true),
'SG' => __('Singapore', true),
'SK' => __('Slovakia (Slovak Republic)', true),
'SI' => __('Slovenia', true),
'SB' => __('Solomon Islands', true),
'SO' => __('Somalia', true),
'ZA' => __('South Africa', true),
'GS' => __('South Georgia and the South Sandwich Islands', true),
'ES' => __('Spain', true),
'LK' => __('Sri Lanka', true),
'SH' => __('St. Helena', true),
'PM' => __('St. Pierre and Miquelon', true),
'SD' => __('Sudan', true),
'SR' => __('Suriname', true),
'SJ' => __('Svalbard and Jan Mayen Islands', true),
'SZ' => __('Swaziland', true),
'SE' => __('Sweden', true),
'CH' => __('Switzerland', true),
'SY' => __('Syrian Arab Republic', true),
'TW' => __('Taiwan, Province of China', true),
'TJ' => __('Tajikistan', true),
'TZ' => __('Tanzania, United Republic of', true),
'TH' => __('Thailand', true),
'TG' => __('Togo', true),
'TK' => __('Tokelau', true),
'TO' => __('Tonga', true),
'TT' => __('Trinidad and Tobago', true),
'TN' => __('Tunisia', true),
'TR' => __('Turkey', true),
'TM' => __('Turkmenistan', true),
'TC' => __('Turks and Caicos Islands', true),
'TV' => __('Tuvalu', true),
'UG' => __('Uganda', true),
'UA' => __('Ukraine', true),
'AE' => __('United Arab Emirates', true),
'GB' => __('United Kingdom', true),
'US' => __('United States', true),
'UM' => __('United States Minor Outlying Islands', true),
'UY' => __('Uruguay', true),
'UZ' => __('Uzbekistan', true),
'VU' => __('Vanuatu', true),
'VE' => __('Venezuela', true),
'VN' => __('Viet Nam', true),
'VG' => __('Virgin Islands (British)', true),
'VI' => __('Virgin Islands (U.S.)', true),
'WF' => __('Wallis and Futuna Islands', true),
'EH' => __('Western Sahara', true),
'YE' => __('Yemen', true),
'YU' => __('Yugoslavia', true),
'ZM' => __('Zambia', true),
'ZW' => __('Zimbabwe', true)
)
)
)
);
}
}
?>
http://bakery.cakephp.org/articles/view/language-select-and-country-select-helper
A question about it - is there any easy way to get the information in reverse, so to get the country when you have the 2 letter code?
Let's say someone registers on my site, and they are from the UK. So, in the DB I have stored 'UK'. But when I show their details, I want it to say 'United Kingdom' instead of 'UK'. I know I can just make an array in the view and use that, but is there a better/more efficient way?
Thanks!
1. name of the helper file and its location
2. we need to include this helper file in any controller like UsersCnntroller with
var $helpers = array('Country');
<?php
/**
* Helper for outputing a country select list.
*
* Allows you to include a selec list of all countries using 1 line of code.
*
* Author: Tane Piper (digitalspaghetti@gmail.com)
* URL: http://digitalspaghetti.me.uk
*
* PHP versions 4 and 5
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
*/
class CountryListHelper extends FormHelper
{
var $helpers = array('Form');
function select($fieldname, $label, $default=" ", $attributes)
{
$list = '<div class="input">';
$list .= $this->Form->label($fieldname, $label);
$list .= $this->Form->select($fieldname , array(
' ' => __('Please select a country', true),
'--' => __('None', true),
'AF' => __('Afganistan', true),
'AL' => __('Albania', true),
'DZ' => __('Algeria', true),
'AS' => __('American Samoa', true),
'AD' => __('Andorra', true),
'AO' => __('Angola', true),
'AI' => __('Anguilla', true),
'AQ' => __('Antarctica', true),
'AG' => __('Antigua and Barbuda', true),
'AR' => __('Argentina', true),
'AM' => __('Armenia', true),
'AW' => __('Aruba', true),
'AU' => __('Australia', true),
'AT' => __('Austria', true),
'AZ' => __('Azerbaijan', true),
'BS' => __('Bahamas', true),
'BH' => __('Bahrain', true),
'BD' => __('Bangladesh', true),
'BB' => __('Barbados', true),
'BY' => __('Belarus', true),
'BE' => __('Belgium', true),
'BZ' => __('Belize', true),
'BJ' => __('Benin', true),
'BM' => __('Bermuda', true),
'BT' => __('Bhutan', true),
'BO' => __('Bolivia', true),
'BA' => __('Bosnia and Herzegowina', true),
'BW' => __('Botswana', true),
'BV' => __('Bouvet Island', true),
'BR' => __('Brazil', true),
'IO' => __('British Indian Ocean Territory', true),
'BN' => __('Brunei Darussalam', true),
'BG' => __('Bulgaria', true),
'BF' => __('Burkina Faso', true),
'BI' => __('Burundi', true),
'KH' => __('Cambodia', true),
'CM' => __('Cameroon', true),
'CA' => __('Canada', true),
'CV' => __('Cape Verde', true),
'KY' => __('Cayman Islands', true),
'CF' => __('Central African Republic', true),
'TD' => __('Chad', true),
'CL' => __('Chile', true),
'CN' => __('China', true),
'CX' => __('Christmas Island', true),
'CC' => __('Cocos (Keeling) Islands', true),
'CO' => __('Colombia', true),
'KM' => __('Comoros', true),
'CG' => __('Congo', true),
'CD' => __('Congo, the Democratic Republic of the', true),
'CK' => __('Cook Islands', true),
'CR' => __('Costa Rica', true),
'CI' => __('Cote d\'Ivoire', true),
'HR' => __('Croatia (Hrvatska)', true),
'CU' => __('Cuba', true),
'CY' => __('Cyprus', true),
'CZ' => __('Czech Republic', true),
'DK' => __('Denmark', true),
'DJ' => __('Djibouti', true),
'DM' => __('Dominica', true),
'DO' => __('Dominican Republic', true),
'TP' => __('East Timor', true),
'EC' => __('Ecuador', true),
'EG' => __('Egypt', true),
'SV' => __('El Salvador', true),
'GQ' => __('Equatorial Guinea', true),
'ER' => __('Eritrea', true),
'EE' => __('Estonia', true),
'ET' => __('Ethiopia', true),
'FK' => __('Falkland Islands (Malvinas)', true),
'FO' => __('Faroe Islands', true),
'FJ' => __('Fiji', true),
'FI' => __('Finland', true),
'FR' => __('France', true),
'FX' => __('France, Metropolitan', true),
'GF' => __('French Guiana', true),
'PF' => __('French Polynesia', true),
'TF' => __('French Southern Territories', true),
'GA' => __('Gabon', true),
'GM' => __('Gambia', true),
'GE' => __('Georgia', true),
'DE' => __('Germany', true),
'GH' => __('Ghana', true),
'GI' => __('Gibraltar', true),
'GR' => __('Greece', true),
'GL' => __('Greenland', true),
'GD' => __('Grenada', true),
'GP' => __('Guadeloupe', true),
'GU' => __('Guam', true),
'GT' => __('Guatemala', true),
'GN' => __('Guinea', true),
'GW' => __('Guinea-Bissau', true),
'GY' => __('Guyana', true),
'HT' => __('Haiti', true),
'HM' => __('Heard and Mc Donald Islands', true),
'VA' => __('Holy See (Vatican City State)', true),
'HN' => __('Honduras', true),
'HK' => __('Hong Kong', true),
'HU' => __('Hungary', true),
'IS' => __('Iceland', true),
'IN' => __('India', true),
'ID' => __('Indonesia', true),
'IR' => __('Iran (Islamic Republic of)', true),
'IQ' => __('Iraq', true),
'IE' => __('Ireland', true),
'IL' => __('Israel', true),
'IT' => __('Italy', true),
'JM' => __('Jamaica', true),
'JP' => __('Japan', true),
'JO' => __('Jordan', true),
'KZ' => __('Kazakhstan', true),
'KE' => __('Kenya', true),
'KI' => __('Kiribati', true),
'KP' => __('Korea, Democratic People\'s Republic of', true),
'KR' => __('Korea, Republic of', true),
'KW' => __('Kuwait', true),
'KG' => __('Kyrgyzstan', true),
'LA' => __('Lao People\'s Democratic Republic', true),
'LV' => __('Latvia', true),
'LB' => __('Lebanon', true),
'LS' => __('Lesotho', true),
'LR' => __('Liberia', true),
'LY' => __('Libyan Arab Jamahiriya', true),
'LI' => __('Liechtenstein', true),
'LT' => __('Lithuania', true),
'LU' => __('Luxembourg', true),
'MO' => __('Macau', true),
'MK' => __('Macedonia, The Former Yugoslav Republic of', true),
'MG' => __('Madagascar', true),
'MW' => __('Malawi', true),
'MY' => __('Malaysia', true),
'MV' => __('Maldives', true),
'ML' => __('Mali', true),
'MT' => __('Malta', true),
'MH' => __('Marshall Islands', true),
'MQ' => __('Martinique', true),
'MR' => __('Mauritania', true),
'MU' => __('Mauritius', true),
'YT' => __('Mayotte', true),
'MX' => __('Mexico', true),
'FM' => __('Micronesia, Federated States of', true),
'MD' => __('Moldova, Republic of', true),
'MC' => __('Monaco', true),
'MN' => __('Mongolia', true),
'MS' => __('Montserrat', true),
'MA' => __('Morocco', true),
'MZ' => __('Mozambique', true),
'MM' => __('Myanmar', true),
'NA' => __('Namibia', true),
'NR' => __('Nauru', true),
'NP' => __('Nepal', true),
'NL' => __('Netherlands', true),
'AN' => __('Netherlands Antilles', true),
'NC' => __('New Caledonia', true),
'NZ' => __('New Zealand', true),
'NI' => __('Nicaragua', true),
'NE' => __('Niger', true),
'NG' => __('Nigeria', true),
'NU' => __('Niue', true),
'NF' => __('Norfolk Island', true),
'MP' => __('Northern Mariana Islands', true),
'NO' => __('Norway', true),
'OM' => __('Oman', true),
'PK' => __('Pakistan', true),
'PW' => __('Palau', true),
'PA' => __('Panama', true),
'PG' => __('Papua New Guinea', true),
'PY' => __('Paraguay', true),
'PE' => __('Peru', true),
'PH' => __('Philippines', true),
'PN' => __('Pitcairn', true),
'PL' => __('Poland', true),
'PT' => __('Portugal', true),
'PR' => __('Puerto Rico', true),
'QA' => __('Qatar', true),
'RE' => __('Reunion', true),
'RO' => __('Romania', true),
'RU' => __('Russian Federation', true),
'RW' => __('Rwanda', true),
'KN' => __('Saint Kitts and Nevis', true),
'LC' => __('Saint LUCIA', true),
'VC' => __('Saint Vincent and the Grenadines', true),
'WS' => __('Samoa', true),
'SM' => __('San Marino', true),
'ST' => __('Sao Tome and Principe', true),
'SA' => __('Saudi Arabia', true),
'SN' => __('Senegal', true),
'SC' => __('Seychelles', true),
'SL' => __('Sierra Leone', true),
'SG' => __('Singapore', true),
'SK' => __('Slovakia (Slovak Republic)', true),
'SI' => __('Slovenia', true),
'SB' => __('Solomon Islands', true),
'SO' => __('Somalia', true),
'ZA' => __('South Africa', true),
'GS' => __('South Georgia and the South Sandwich Islands', true),
'ES' => __('Spain', true),
'LK' => __('Sri Lanka', true),
'SH' => __('St. Helena', true),
'PM' => __('St. Pierre and Miquelon', true),
'SD' => __('Sudan', true),
'SR' => __('Suriname', true),
'SJ' => __('Svalbard and Jan Mayen Islands', true),
'SZ' => __('Swaziland', true),
'SE' => __('Sweden', true),
'CH' => __('Switzerland', true),
'SY' => __('Syrian Arab Republic', true),
'TW' => __('Taiwan, Province of China', true),
'TJ' => __('Tajikistan', true),
'TZ' => __('Tanzania, United Republic of', true),
'TH' => __('Thailand', true),
'TG' => __('Togo', true),
'TK' => __('Tokelau', true),
'TO' => __('Tonga', true),
'TT' => __('Trinidad and Tobago', true),
'TN' => __('Tunisia', true),
'TR' => __('Turkey', true),
'TM' => __('Turkmenistan', true),
'TC' => __('Turks and Caicos Islands', true),
'TV' => __('Tuvalu', true),
'UG' => __('Uganda', true),
'UA' => __('Ukraine', true),
'AE' => __('United Arab Emirates', true),
'GB' => __('United Kingdom', true),
'US' => __('United States', true),
'UM' => __('United States Minor Outlying Islands', true),
'UY' => __('Uruguay', true),
'UZ' => __('Uzbekistan', true),
'VU' => __('Vanuatu', true),
'VE' => __('Venezuela', true),
'VN' => __('Viet Nam', true),
'VG' => __('Virgin Islands (British)', true),
'VI' => __('Virgin Islands (U.S.)', true),
'WF' => __('Wallis and Futuna Islands', true),
'EH' => __('Western Sahara', true),
'YE' => __('Yemen', true),
'YU' => __('Yugoslavia', true),
'ZM' => __('Zambia', true),
'ZW' => __('Zimbabwe', true)
), $default, $attributes);
$list .= '</div>';
return $this->output($list);
}
}
?>
code country_list.php
class CountryListHelper extends FormHelper
{
var $helpers = array('Form');
function select($fieldname)
{
$list = $this->Form->input($fieldname , array('type' => 'select', 'label' => 'Please select a country', 'options' => array(
'' => 'Please select a country',
'AF' => 'Afganistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
'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 Herzegowina',
'BW' => 'Botswana',
'BV' => 'Bouvet Island',
'BR' => 'Brazil',
'IO' => 'British Indian Ocean Territory',
'BN' => 'Brunei Darussalam',
'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',
'CG' => 'Congo',
'CD' => 'Congo, the Democratic Republic of the',
'CK' => 'Cook Islands',
'CR' => 'Costa Rica',
'CI' => 'Cote d\'Ivoire',
'HR' => 'Croatia (Hrvatska)',
'CU' => 'Cuba',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'DK' => 'Denmark',
'DJ' => 'Djibouti',
'DM' => 'Dominica',
'DO' => 'Dominican Republic',
'TP' => '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',
'FX' => 'France, Metropolitan',
'GF' => 'French Guiana',
'PF' => 'French Polynesia',
'TF' => 'French Southern Territories',
'GA' => 'Gabon',
'GM' => 'Gambia',
'GE' => 'Georgia',
'DE' => 'Germany',
'GH' => 'Ghana',
'GI' => 'Gibraltar',
'GR' => 'Greece',
'GL' => 'Greenland',
'GD' => 'Grenada',
'GP' => 'Guadeloupe',
'GU' => 'Guam',
'GT' => 'Guatemala',
'GN' => 'Guinea',
'GW' => 'Guinea-Bissau',
'GY' => 'Guyana',
'HT' => 'Haiti',
'HM' => 'Heard and Mc Donald Islands',
'VA' => 'Holy See (Vatican City State)',
'HN' => 'Honduras',
'HK' => 'Hong Kong',
'HU' => 'Hungary',
'IS' => 'Iceland',
'IN' => 'India',
'ID' => 'Indonesia',
'IR' => 'Iran (Islamic Republic of)',
'IQ' => 'Iraq',
'IE' => 'Ireland',
'IL' => 'Israel',
'IT' => 'Italy',
'JM' => 'Jamaica',
'JP' => 'Japan',
'JO' => 'Jordan',
'KZ' => 'Kazakhstan',
'KE' => 'Kenya',
'KI' => 'Kiribati',
'KP' => 'Korea, Democratic People\'s Republic of',
'KR' => 'Korea, Republic of',
'KW' => 'Kuwait',
'KG' => 'Kyrgyzstan',
'LA' => 'Lao People\'s Democratic Republic',
'LV' => 'Latvia',
'LB' => 'Lebanon',
'LS' => 'Lesotho',
'LR' => 'Liberia',
'LY' => 'Libyan Arab Jamahiriya',
'LI' => 'Liechtenstein',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'MO' => 'Macau',
'MK' => 'Macedonia, The Former Yugoslav 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, Federated States of',
'MD' => 'Moldova, Republic of',
'MC' => 'Monaco',
'MN' => 'Mongolia',
'MS' => 'Montserrat',
'MA' => 'Morocco',
'MZ' => 'Mozambique',
'MM' => 'Myanmar',
'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',
'MP' => 'Northern Mariana Islands',
'NO' => 'Norway',
'OM' => 'Oman',
'PK' => 'Pakistan',
'PW' => 'Palau',
'PA' => 'Panama',
'PG' => 'Papua New Guinea',
'PY' => 'Paraguay',
'PE' => 'Peru',
'PH' => 'Philippines',
'PN' => 'Pitcairn',
'PL' => 'Poland',
'PT' => 'Portugal',
'PR' => 'Puerto Rico',
'QA' => 'Qatar',
'RE' => 'Reunion',
'RO' => 'Romania',
'RU' => 'Russian Federation',
'RW' => 'Rwanda',
'KN' => 'Saint Kitts and Nevis',
'LC' => 'Saint LUCIA',
'VC' => 'Saint Vincent and the Grenadines',
'WS' => 'Samoa',
'SM' => 'San Marino',
'ST' => 'Sao Tome and Principe',
'SA' => 'Saudi Arabia',
'SN' => 'Senegal',
'SC' => 'Seychelles',
'SL' => 'Sierra Leone',
'SG' => 'Singapore',
'SK' => 'Slovakia (Slovak Republic)',
'SI' => 'Slovenia',
'SB' => 'Solomon Islands',
'SO' => 'Somalia',
'ZA' => 'South Africa',
'GS' => 'South Georgia and the South Sandwich Islands',
'ES' => 'Spain',
'LK' => 'Sri Lanka',
'SH' => 'St. Helena',
'PM' => 'St. Pierre and Miquelon',
'SD' => 'Sudan',
'SR' => 'Suriname',
'SJ' => 'Svalbard and Jan Mayen Islands',
'SZ' => 'Swaziland',
'SE' => 'Sweden',
'CH' => 'Switzerland',
'SY' => 'Syrian Arab Republic',
'TW' => 'Taiwan, Province of China',
'TJ' => 'Tajikistan',
'TZ' => 'Tanzania, United Republic of',
'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',
'VE' => 'Venezuela',
'VN' => 'Viet Nam',
'VG' => 'Virgin Islands (British)',
'VI' => 'Virgin Islands (U.S.)',
'WF' => 'Wallis and Futuna Islands',
'EH' => 'Western Sahara',
'YE' => 'Yemen',
'YU' => 'Yugoslavia',
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe'
), 'error' => 'Please select a country'));
return $this->output($list);
}
}
?>
page views
echo $countryList->select('country');
I hope it is useful for everyone.
first of all: A great idea, I love this helper!
I'm using ist in my "add" and "edit" forms. But here's my issue:
How can I pass the current value from the database into it?
Tried this one:
e( $stateList->select('state', ' ', $this->data['Department']['state'], 'Bitte wählen Sie ein Bundesland aus.', ''));
this one
e( $stateList->select('state', ' ', $data[Department][state], 'Bitte wählen Sie ein Bundesland aus.', ''));
and anything else, always getting:
"Fatal error: Cannot unset string offsets in /var/www/cake/libs/view/helpers/form.php"
thanks and keep up the great work!
hi,
i have the same issue. did you ever figure out how to get the value from the database into your drop down? seems like i can't figure it out. would be great if you could post your solution. thanks.
select('country', 'Please select your country'));?>
but still I am getting this error:
Notice (8): Undefined variable: countryList [APP\views\users\register.ctp, line 23]
Fatal error: Call to a member function select() on a non-object in C:\xampp\htdocs\video\app\views\users\register.ctp on line 23
Please suggest what the wrong i am doing...just newbie in cake.. :)
Its fixed....I forgot to add the variable in the controller's helper array..
$list .= $this->Form->label($label);Per this:
$list .= $this->Form->label($fieldname,$label);so the html is formatted properly
When using this helper, the required => true is not being picked up from the model.
Is there a param that needs to be sent to set the required = true?
Update: nevermind, I figured it out. I added a new param for a required option.
and when I call it, I pass the string 'required' as a param.function select($fieldname, $label, $default=" ", $required="", $attributes=array())
{
$list = '<div class="input '.$required.'">';
Update: Nevermind, I figured it out. I missed the default param.
class StateListHelper extends FormHelper
{
var $helpers = array('Form');
function select($fieldname, $label, $default=" ", $attributes = null)
{
$list = '<div class="input">';
$list .= $this->Form->label($label);
$list .= $this->Form->select($fieldname ,array(
'AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
'CA'=>"California",
'CO'=>"Colorado",
'CT'=>"Connecticut",
'DE'=>"Delaware",
'DC'=>"District Of Columbia",
'FL'=>"Florida",
'GA'=>"Georgia",
'HI'=>"Hawaii",
'ID'=>"Idaho",
'IL'=>"Illinois",
'IN'=>"Indiana",
'IA'=>"Iowa",
'KS'=>"Kansas",
'KY'=>"Kentucky",
'LA'=>"Louisiana",
'ME'=>"Maine",
'MD'=>"Maryland",
'MA'=>"Massachusetts",
'MI'=>"Michigan",
'MN'=>"Minnesota",
'MS'=>"Mississippi",
'MO'=>"Missouri",
'MT'=>"Montana",
'NE'=>"Nebraska",
'NV'=>"Nevada",
'NH'=>"New Hampshire",
'NJ'=>"New Jersey",
'NM'=>"New Mexico",
'NY'=>"New York",
'NC'=>"North Carolina",
'ND'=>"North Dakota",
'OH'=>"Ohio",
'OK'=>"Oklahoma",
'OR'=>"Oregon",
'PA'=>"Pennsylvania",
'RI'=>"Rhode Island",
'SC'=>"South Carolina",
'SD'=>"South Dakota",
'TN'=>"Tennessee",
'TX'=>"Texas",
'UT'=>"Utah",
'VT'=>"Vermont",
'VA'=>"Virginia",
'WA'=>"Washington",
'WV'=>"West Virginia",
'WI'=>"Wisconsin",
'WY'=>"Wyoming"), $default, $attributes);
$list .= '</div>';
return $this->output($list);
}
}
Actually, this helper doesn't even take advantage of extending the FormHelper class anyway. It does use Helper::output() properly.
Warning: Missing argument 4 for CountryListHelper::select(), called in C:\wamp\www\app\views\users\register.thtml on line 35 and defined in C:\wamp\www\app\views\helpers\country_list.php on line 23
Do I need to add the 3rd and 4th parameter?
Thanks
When you change the function line from
function select($fieldname, $label, $default=" ", $attributes)
to
function select($fieldname, $label, $default = " ", $attributes = array() )
there will be no more need to add the 3rd and 4th parameter
hf
I'm attempting to use your excellent country list helper but I'm running into some problems. I have the following in my view:
e($countryList->select('bill_state_prov', ' ','',''));
I have added the country_list.php file to the app/views/helpers directory. I keep getting the following error when I reload my form:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/roliver/workspace/galleries/trunk/cake/libs/view/helpers/form.php on line 615
Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /home/roliver/workspace/galleries/trunk/cake/libs/view/helpers/form.php on line 623
Any clues, I would be very grateful. Oh, I'm using 1.2 alpha not SVN alpha
Comments are closed for articles over a year old