Codeigniter Country Drop Down List Form Helper

Firstly, I create a helper and name it as MY_form_helper.php. This file will go into the application/helpers directory. I have extended the Form Helper as I will be using the country drop down select box in my forms.

[php]
function country_dropdown($countries, $name=”country”, $top_countries=array(), $selection=NULL, $show_all=TRUE)
{
$html = “<select name='{$name}’ id='{$name}’>”;
$selected = NULL;
if (in_array($selection, $top_countries)) {
$top_selection = $selection;
$all_selection = NULL;
} else {
$top_selection = NULL;
$all_selection = $selection;
}

if (!empty($top_countries)) {
foreach ($top_countries as $value)
{
if (array_key_exists($value, $countries)) {
if ($value === $top_selection) {
$selected = “SELECTED”;
}
$html .= “<option value='{$value}’ {$selected}>{$countries[$value]}</option>”;
$selected = NULL;
}
}
$html .= “<option>———-</option>”;
}

if ($show_all) {
foreach ($countries as $key => $country)
{
if ($key === $all_selection) {
$selected = “SELECTED”;
}
$html .= “<option value='{$key}’ {$selected}>{$country}</option>”;
$selected = NULL;
}
}

$html .= “</select>”;
return $html;
}
[/php]

The above code was originally posted by Watermark Studios in the Codeigniter forum. There is also a Codeigniter Wiki article that explains the same.

Now, we need the country data. At the above links, you will find an array of countries with their ISO codes and names as key and value pairs. I chose to go for a database approach. I have a database table called ‘countries’, that has the names, ISO 2 and ISO 3 codes of the world countries. You can download the SQL script to create this table. CountriesSQL.zip. Please note : This is not a comprehensive or perfect list of countries. You can find many similar database tables from around the web.

The following PHP function goes into the Codeigniter Datamapper Country model. It basically pulls out the ISO 2 codes and names of all the countries and creates an array of key and value pairs.

[php]
function get_dropdownlist()
{
$countries = array();
$countries_get = $this->get();
foreach ($countries_get as $country)
{
$countries[$country->iso_code_2] = $country->name;
}
return $countries;
}
[/php]

In the Codeigniter controller, I load the Country model and pass the data to the view through a $countries variable.

[php]
// Loading the Country model
$countries = new Country();
// Sending the drop down list to the view
$this->template->countries = $countries->get_dropdownlist();
[/php]

Within the view, I echo the $countries via the country_dropdown() helper function. The first parameter is the array containing the country names and their ISO 2 Codes. The second is the name of the select form field. The third is an array of countries that I wish to be placed on the top of the select field. The last parameter is the country that I wish to preselect.

[php]
<?php echo country_dropdown($countries, ‘iso_code_2’, array(‘GB’, ‘US’), ‘GB’; ?>
[/php]

Codeigniter Countries Drop Down Select Form Field

This is a screenshot is how my country drop down form field looks like.