Welcome Guest, Not a member yet? Register   Sign In
Flags Helper - Select Country Dropdown
#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.


Messages In This Thread
Flags Helper - Select Country Dropdown - by El Forum - 12-25-2009, 09:44 PM
Flags Helper - Select Country Dropdown - by El Forum - 12-26-2009, 06:34 AM
Flags Helper - Select Country Dropdown - by El Forum - 12-27-2009, 06:30 AM
Flags Helper - Select Country Dropdown - by El Forum - 12-28-2009, 05:53 PM
Flags Helper - Select Country Dropdown - by El Forum - 12-29-2009, 08:53 PM
Flags Helper - Select Country Dropdown - by El Forum - 12-29-2009, 08:58 PM
Flags Helper - Select Country Dropdown - by El Forum - 01-15-2010, 11:45 PM
Flags Helper - Select Country Dropdown - by El Forum - 05-16-2013, 01:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB