Welcome Guest, Not a member yet? Register   Sign In
Flags Helper - Select Country Dropdown

Hello there. I made a basic helper to be used with CodeIgniter when you need a list of countries (useful for user registration, for example).

The Country Names are coded in ISO 3166-1 alpha-2.

The usage is really simple:

# Load the helper

# echo the dropdown select
# @param string $field_name = is the name of the field.
# @param string $country_selected = is the default value for the field.
echo select_countries('flags', 'pa');

You can use and abuse it and you can find the code at GitHub


[eluser]Mahyar Ss[/eluser]
hi demogar,

very useful

i need it


Excellent! Thank you! Smile

[eluser]m4d3 Gun[/eluser]
thank's for simple n usefull helper, i already used it...... n work fine.

Thank you guys. I think I'll convert it in a Library (or add more functions to the helper as well) to get more functionality like get the Country Name if we use the country code and so.

I'll keep you updated.

[eluser]Watermark Studios[/eluser]
I did something similar, but I wanted an array to be passed as a parameter that directs the helper to customize the first few options. Most companies only operate in a few countries, but they want to allow all countries to be available. So I created the below help extension.

Just create a file in application/helpers called MY_form_helper.php

You can load the helper like you would normally load a helper in a controller
and call the function in a view
<?php echo country_dropdown(); ?>
The first parameter in the function is the name of the selection element and the second is an array of the countries in ISO 3166-1-alpha-2 code elements. For example
<?php echo country_dropdown('country',
would return a list of all countries in the ISO 3166 list with the 10 countries in the array at the top of the list.

Below is the function for MY_form_helper.php
function country_dropdown($name="country", $top_countries=array()){
  $countries = array(
// list of countries continues until Zimbabwe
  $html = "<select name='{$name}'>";
    foreach($top_countries as $value){
      if(array_key_exists($value, $countries)){
        $html .= "<option value='{$value}'>{$countries[$value]}</option>";
    $html .= "<option>----------</option>";
  foreach($countries as $key => $country){
    $html .= "<option value='{$key}'>{$country}</option>";
  $html .= "</select>";
  return $html;

Very similar to how yours worked, but with the option to choose the top options which I think is critical for international companies. Also, it is important to find an array of up-to-date ISO 3166 codes. There are 222 codes on the list, I'd hate for you to have to create it like I did (couldn't find an up-to-date version until I saw this post.

Thnx demogar and watermark it worked for me.

Theme © iAndrew 2016 - Forum software by © MyBB