Zend Framework:将数据库数据填充到 Zend Form 下拉元素

发布于 2024-09-26 07:53:24 字数 828 浏览 6 评论 0原文

我有以下形式:

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捂风挽笑 2024-10-03 07:53:25

如果您的列表内容取决于某些条件,您可以通过控制器的操作来完成此操作(或者甚至在服务层中,如果要小心的话)。
用法:

$form->getElement('country')->addMultiOption('1','USA');     //add single value
$form->getElement('country')->addMultiOptions(array('1'=>'USA', '2'=>'Canada'));     //add values by array
$form->getElement('country')->setMultiOptions(array('1'=>'USA', '2'=>'Canada'));     //set values by array

当然,要从数据库添加值,您需要先获取它们。

请参阅 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:

$form->getElement('country')->addMultiOption('1','USA');     //add single value
$form->getElement('country')->addMultiOptions(array('1'=>'USA', '2'=>'Canada'));     //add values by array
$form->getElement('country')->setMultiOptions(array('1'=>'USA', '2'=>'Canada'));     //set values by array

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.

貪欢 2024-10-03 07:53:25

最好的方法是为元素创建一个新类:

将其放入“/application/form/element/CountySelect.php”中

class Application_Form_Element_CountrySelect extends Zend_Form_Element_Select {
    public function init() {
        $oCountryTb = new Application_Model_Country();
        $this->addMultiOption(0, 'Please select...');
        foreach ($oCountry->fetchAll() as $oCountry) {
            $this->addMultiOption($oCountry['id'], $oCountry['name']);
        }
    }
}

,然后将其添加到表单中:

class Application_Form_RegistrationForm extends Zend_Form{
    public function init() {
        $this->addElement(new Application_Form_Element_CountrySelect('country_id'));
    }
}

The best way is to create a new class for the element:

Put this in "/application/form/element/CountySelect.php"

class Application_Form_Element_CountrySelect extends Zend_Form_Element_Select {
    public function init() {
        $oCountryTb = new Application_Model_Country();
        $this->addMultiOption(0, 'Please select...');
        foreach ($oCountry->fetchAll() as $oCountry) {
            $this->addMultiOption($oCountry['id'], $oCountry['name']);
        }
    }
}

And then add it to the form this way:

class Application_Form_RegistrationForm extends Zend_Form{
    public function init() {
        $this->addElement(new Application_Form_Element_CountrySelect('country_id'));
    }
}
神爱温柔 2024-10-03 07:53:24

你当然可以。

在 init 方法中,您可以使用如下方式设置选项,假设 $db 是 Zend_Db 适配器:

$options = $db->fetchPairs('SELECT id, name FROM country ORDER BY name ASC');
$country->setMultiOptions($options);

如果您还没有看到 fetchPairs 方法,它会构建一个数组,其中第一列返回成为键,第二列返回成为键。列该值。

You sure can.

In the init method you can set the options with something like this, assuming $db is a Zend_Db adapter:

$options = $db->fetchPairs('SELECT id, name FROM country ORDER BY name ASC');
$country->setMultiOptions($options);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文