开发者

How to fill addMultiOptions with data from database

What I want to create is a form with an select field. The items in the select field are categories, I want to pu开发者_StackOverflow社区t them from the database into the form addMultiOptions.

Hope some one can help me.

With kind regards,

Nicky


If you use Zend_Db_Table_Abstract, and Zend Form, you could do this:

$result = $model->fetchAll()->toArray();
$options = array();

foreach ($result as $value) {
    $options[$value['id']] = $value['whatEver'];
}

$field = new Zend_Form_Element_Select();
$field->setMultiOptions($options);

Or from an Controller:

$model = new My_Model_WhatEver();
$result = $model->fetchAll()->toArray();
$options = array();

foreach ($result as $value) {
    $options[$value['id']] = $value['whatEver'];
}

$form = new My_Form_WhatEver();
$element = $form->getElement('whatEver');
$element->setMultiOptions($result);


You can use one or more of the Zend_Form_Element_Multi methods. If you have an array in the form

array(
  'id1' => 'value 1',
  'id2' => 'value 2',
);

You can add all of them by using setMultiOptions method. If you instead need to add options inside a foreach loop, you have to use addMultiOption method which add an option at time with

foreach ($array as $id => $value) {
  $element->addMultiOption($id, $value);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜