Welcome Guest, Not a member yet? Register   Sign In
Easy country drop-down using a form helper extension - ISO 3166
#11

[eluser]brucebat[/eluser]
Im getting an error on CI 2.02 saying:

Quote:Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/country_helper.php

Line Number: 261

I have given the helper the countries array which is initialized inside the helper before the function.

I used your example function call and it is not working.


Code:
if($show_all)  {
        foreach($countries as $key => $country)  {
            if($key === $all_selection)  {
                $selected = "SELECTED";
                }
            $html .= "<option value='{$key}' {$selected}>{$country}</option>";
            $selected = NULL;
            }
        }
#12

[eluser]Michael Layne[/eluser]
Same here - Cannot get this to work in 2.0.2. What am I missing? Thanks. Great helper BTW.

Michael
#13

[eluser]mi6crazyheart[/eluser]
I've little tweaked this one. Have a look at here: http://goo.gl/KIx9x
#14

[eluser]Michael Layne[/eluser]
brucebat,

I had another look and turned out I didn't have my array in my config/config.php file for this new app. I looked to see what was different with a completed recent app and that was it.. So... to be clear, I added this ast the end of my config.php file:
Code:
$config['country_list'] = array(
  "AF"=>"Afghanistan",
  "AL"=>"Albania",
...

HTH
#15

[eluser]oniadvanced[/eluser]
Hi, Guys.

Excellent work with this helper.

I just did some changes to the helper in order to organize more the code.

First, I created a config file (and put them in applications/config/form.php) to save two variables. Take a look:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* --------------------------
* Config: Country List
* --------------------------
* Default value:
* $config['country_list'] = array();
*
* Contains the complete list of countries
*/
$config['country_list'] = array(
"AF"=>"Afghanistan",
"AL"=>"Albania",
"DZ"=>"Algeria",
//here add the complete list from the wiki
"ZM"=>"Zambia",
"ZW"=>"Zimbabwe");

/**
* --------------------------
* Config: Top Country List
* --------------------------
* Default value:
* $config['country_list'] = array('US','CA','GB','DE','BR','IT','ES','AU','NZ','HK');
*
* Contains the top country list
*/
$config['top_countries'] = array('US','CA','GB','DE','BR','IT','ES','AU','NZ','HK');
?&gt;

ok, now in the helper we need to load this file. As you see here:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('country_dropdown'))
{
    function  country_dropdown ( $name="country", $top_countries=array(), $selection=NULL, $show_all=TRUE )  {
        // You may want to pull this from an array within the helper
        $CI =& get_instance();
        $CI->config->load('form');
        $countries = $CI->config->item('country_list');
        
        if (empty($top_countries))
            $top_countries = $CI->config->item('top_countries');
    
        $html = "<select name='{$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;
    }
}
?&gt;

We can improve it a little more but is ok for now, maybe in a few days I'll add some extra functions.

I hope this helps to others as It helps me. Code Igniter Rocks!
#16

[eluser]jedd[/eluser]
[quote author="oniadvanced" date="1310448531"]
I just did some changes to the helper in order to organize more the code.
[/quote]

Please update the wiki entry
[url="http://codeigniter.com/wiki/helper_dropdown_country_code"]Helper - dropdown - country code[/url]

It's easier to find it in the wiki than in a forum thread.
#17

[eluser]Vamsii[/eluser]
Thanks ! time saver Big Grin
#18

[eluser]Unknown[/eluser]
I appear to be having no luck getting this to work, all I am getting when I put the echo in the view is a undefined call to function country_dropdown. Anyone else experienced this?

"Fatal error: Call to undefined function country_dropdown() in /Users/gavin/Sites/Jowst/themes/admin/views/teams/add.php on line 39"

LIne 39 being the echo country_dropdown();


UPDATE: Managed to fix this, wasn't loading the helper in autoload.

Next problem is selecting the current value in the DB. Did anyone manage to solve this as I noticed it was mentioned above?
#19

[eluser]touson[/eluser]
Well done chaps, saved me tonnes of time and effort.

I love this community....
#20

[eluser]InsiteFX[/eluser]
I have them in a database for easy lookup.




Theme © iAndrew 2016 - Forum software by © MyBB