HTML Helper for generating a country drop-down list in CodeIgniter
on Friday 9th May, 2008 Gabe speculated thusly…Frustrated at having to create my own drop-down country lists in CI I created a helper for it. It can even work with the CI Validation class, so if the form is submitted and fails validation when it is redisplayed the same country is selected as the form was submitted with.
Download the file MY_form_helper.php and put it in the folder: /system/application/helpers/.
Download the file countries.php and place that in /system/application/config/.
You will now be able to create drop-down lists of countries in your views like this:
< ?=form_countries( 'country', $this->validation->country, array( 'style' => 'width: 250px' ) )?>
Ensure that you create a validation rule called “country” in your controller and that you are loading the form helper.
form_countries() takes three arguments. The first is the name you would like to give the select box, this is required. The second is the country to select by default – this could be something like ‘GB’, or $this->validation->country, otherwise you can leave it blank. The third argument is any additional properties to give the select box, you can leave this blank, or pass an array. In the example above we also set the width to 250x. The country chosen by the user will be returned to the script as a two-letter ISO country code, if you are validating this then you can set the min and max length values as 2, and accept alphabetical characters only. If using MySQL as your database you will get best performance by creating a column of the type char with a length of 2.
Excellent! Looks great. If you haven’t yet, consider making a post on the wiki about this.
The links are broken!
Thanks for letting me know, Atul. The links should be working again. Hope that helps.
Thanks, Keep it up…
Great help provided.. very thankful to you…keep it up