/    Sign up×
Community /Pin to ProfileBookmark

Zend Framework: Populating DB data to a Zend Form dropdown element

Hi

I have the following form:

[CODE]<?php
class Application_Form_RegistrationForm extends Zend_Form{

public function init(){

$country = $this->createElement(‘select’, ‘country’);
$country->setLabel(‘country: ‘)
->setRequired(true);

$email = $this->createElement(‘text’, ’email_address’);
$email->setLabel(‘Email Address: ‘)
->setRequired(true);

$register = $this->createElement(‘submit’, ‘register’);
$register->setLabel(‘Create new Account’)
->setIgnore(true);

$this->addElements(array(
$country, $email, $register
));

}

}

?>[/CODE]

The list of the countries are present in a table [B]country[/B] in a database.

Is there anyway I can populate the country dropdown list with the country names from the database?

Any help is appreciated.

Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 08.2010 — I think (not a Zend user) you would query the DB, put the results into an array where the keys will be the "value" attribute of each option and the values will be the displayed text, then supply that array as the argument to the addMultiOptions() method for that form element object.
Copy linkTweet thisAlerts:
@phantom007authorOct 09.2010 — Logically I also have the same idea, but dont know where to put which code LOL
Copy linkTweet thisAlerts:
@phantom007authorOct 09.2010 — Nevermind, I found the solution:


[code=php] $db = new Application_Model_Countries();
$data = $db->getCountries();

$country = $this->createElement('select', 'country');
$country->addMultiOptions($data)
->removeDecorator('label');[/code]
×

Success!

Help @phantom007 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 5.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...