Welcome Guest, Not a member yet? Register   Sign In
Using the country dropdown helper
#1

[eluser]invision[/eluser]
Hi,

I'd like to use the following: http://codeigniter.com/wiki/helper_dropd...ntry_code/ in a form.

How do I achieve this?

Where does the Helper go?


Thank you.
#2

[eluser]smilie[/eluser]
Try: http://ellislab.com/codeigniter/user-gui...lpers.html

Cheers,
Smilie
#3

[eluser]invision[/eluser]
Thanks for the reply.

I'm a little unsure how to use it.

Should I have country_helper.php as:

Code:
<?php
$config['country_list'] = array(
"AF"=>"Afghanistan",
...
"ZW"=>"Zimbabwe");

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
    $countries = config_item('country_list');

    $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;

and my View as :

Code:
echo country_dropdown('country');

and my Controller as :

Code:
$this->load->helper(array('form', 'url', 'country'));
?


Thanks for any help with this Smile
#4

[eluser]smilie[/eluser]
Hi,

By looking at the code, I would say that that is correct. I did not test it however Smile

Basically, helper files are 'simple' functions that do something. Once you load helper (which you do in controller and / or view) you can access all functions from that helper.

I would say - try and see what happens Smile

Cheers,
Smilie
#5

[eluser]invision[/eluser]
Hi Smilie Smile
Thanks for the quick reply.

I tried this exact setup, but got:

Code:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/country_helper.php
Line Number: 256

Any ideas?

256 is this line:
foreach($countries as $key => $country) {


Thank you
#6

[eluser]smilie[/eluser]
Ah, I see.

Just place $config['country_list'] = array (...) WITHIN the function itself Smile
When it is outside the function, function does not know about it Smile

Let me know if that worked Smile

Cheers,
Smilie
#7

[eluser]invision[/eluser]
Thanks for the reply.

I have now moved the array within the function but get the same error :'(
#8

[eluser]smilie[/eluser]
Hm, according to this thread: http://ellislab.com/forums/viewthread/141730/ it should work... Only difference I see is that they have named it MY_form_helper.php :S Do not know if that MY_ does some magic or not.

You could try to move complete $config[] array to application/config/config.php

Cheers,
Smilie
#9

[eluser]invision[/eluser]
Wow, that did the trick!

I moved the $config[] to config.php and it now works Big Grin

Many thanks Smilie Big Grin
#10

[eluser]smilie[/eluser]
Sure no problem.
Should tho' work in the function itself. As creator of this helper said in other topic, they did not want to 'overload' the config.php with such long array Smile

Have fun!

Cheers,
Smilie




Theme © iAndrew 2016 - Forum software by © MyBB