/    Sign up×
Community /Pin to ProfileBookmark

making a country dropdown list in cake

Hi,

I am trying to make a select dropdown list of countries in cakephp.

Now for doing this I am following this below tutorial

[url]http://bakery.cakephp.org/articles/view/country-select-list-helper[/url]

But I am getting

Notice (8): Undefined variable: countryList

Fatal error: Call to a member function select() on a non-object

Could you please let me know what the wrong I am doing.

I have placed the helper inside helper dir and calling the function inside the user view.

Please suggest.

Thanks,
Raj

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiAug 14.2009 — Make sure you have included the helper in your controller class' $helpers property.
Copy linkTweet thisAlerts:
@raj_2006authorAug 14.2009 — Hey,

Its a great help from you.

I didn't include CountryList in the usercontroller...

Thank you so much...... ?

Only the first 2 alphabets will be inserted in the db table.Now when I will fetch the record then how can I display the full name of the country.

Such as now its AR for Argentina.So will I go one by one using a if else loop (it will be a long and timespending matter)in the views? or is there any shorcut method.

Please suggest.

Thanks,

Raj
Copy linkTweet thisAlerts:
@MindzaiAug 14.2009 — You could just store all the countries in an associative array:

[code=php]
$countries = array(
'ar' => 'Argentina',
'br' => 'Brazil'
// etc
)
[/code]


Then access them like so:

[code=php]echo $countries[$data['Model']['country']];[/code];

Though I'd have though it would make more sense to have a Country model and then just store the foreign key in your main model's table. From there you can just define a Model hasOne Country associatoin and get the data automatically.
Copy linkTweet thisAlerts:
@raj_2006authorAug 14.2009 — ya the countries are listed in a associative array and also I don't want to create any country table..so without using table is it possible to fetch the country name from the array for the actual country code?

The array is residing inside the helpers.
Copy linkTweet thisAlerts:
@raj_2006authorAug 16.2009 — Hi,

I am trying to validate the select box but I think its not directing to user model.

What I did:

[code=php]//User model

var $validate = array('country' => array(
// must not be empty
'rule' => VALID_NOT_EMPTY,
'required' => true ,
// error message to display
'message' => 'Please enter country'
)
);

//View:

echo $countryList->select('country', 'Please select your country:','required');

// Helper countrylist

function select($fieldname, $label, $default=" ",$required=" ",$attributes = array())
{
$list = '<div class="input '.$required.'">';
.........[/code]


Also if I select one country then after validation the select field display the default option..I think it can't hold back the post data...

Plz suggest me...what mistake i am doing...
×

Success!

Help @raj_2006 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...