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

[eluser]demogar[/eluser]
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:

Code:
# Load the helper
$this->load->helper("flags");

# 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
#2

[eluser]Sbioko[/eluser]
Thanks!
#3

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

very useful

i need it

thnX
#4

[eluser]coldKingdom[/eluser]
Excellent! Thank you! Smile
#5

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

[eluser]demogar[/eluser]
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.
#7

[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
Code:
$this->load->helper('form');
and call the function in a view
Code:
<?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
Code:
<?php echo country_dropdown('country',
                            array('US','CA','GB','DE','BR','IT','ES','AU','NZ','HK'));?>
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
Code:
function country_dropdown($name="country", $top_countries=array()){
  $countries = array(
        "AF"=>"Afghanistan",
        "AL"=>"Albania",
        "DZ"=>"Algeria",
        "AD"=>"Andorra",
        "AO"=>"Angola",
        "AI"=>"Anguilla",
        "AQ"=>"Antarctica",
// list of countries continues until Zimbabwe
        "ZW"=>"Zimbabwe"
        );
  $html = "<select name='{$name}'>";
  if(!empty($top_countries)){
    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.
#8

[eluser]Unknown[/eluser]
Thnx demogar and watermark it worked for me.




Theme © iAndrew 2016 - Forum software by © MyBB