Zend Framework:将数据库数据填充到 Zend Form 下拉元素
我有以下形式:
<?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
));
}
}
?>
国家列表存在于数据库中的表country
中。
无论如何,我可以使用数据库中的国家/地区名称填充国家/地区下拉列表吗?
任何帮助表示赞赏。
谢谢
I have the following form:
<?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
));
}
}
?>
The list of the countries are present in a table country
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的列表内容取决于某些条件,您可以通过控制器的操作来完成此操作(或者甚至在服务层中,如果要小心的话)。
用法:
当然,要从数据库添加值,您需要先获取它们。
请参阅 http://framework.zend。 com/manual/en/zend.form.standardElements.html#zend.form.standardElements.select 了解更多可用方法。
You could do it from controller's action (or even in Service Layer, if to be meticulous), if your list's content depends from some conditions.
Usage:
Of course, to add values from DB you need to fetch them first.
See http://framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.select for more methods available.
最好的方法是为元素创建一个新类:
将其放入“/application/form/element/CountySelect.php”中
,然后将其添加到表单中:
The best way is to create a new class for the element:
Put this in "/application/form/element/CountySelect.php"
And then add it to the form this way:
你当然可以。
在 init 方法中,您可以使用如下方式设置选项,假设 $db 是 Zend_Db 适配器:
如果您还没有看到 fetchPairs 方法,它会构建一个数组,其中第一列返回成为键,第二列返回成为键。列该值。
You sure can.
In the init method you can set the options with something like this, assuming $db is a Zend_Db adapter:
In case you haven't seen the fetchPairs method, it builds an array, where the first column return becomes the key, and the second column the value.