如何在Zend Framework + MySQL表中创建一个select - 选项

时间:2013-12-24 02:04:51

标签: zend-framework zend-form zend-db zend-db-table

如何在Zend Framework + MySQL表中创建一个select - 选项

控制器:

$persons = new Application_Model_DbTable_Persons();
$data_persons = $persons->findPersons();

控制器和视图?如何? o.O

2 个答案:

答案 0 :(得分:0)

在控制器

   $this->view->dataPerson = $data_persons;

查看Html。

  foreach($this->dataPerson as $datPers):
               //Example Get person name.
          echo $this->escape($datPers->name);
           and etc.

 endforeach;

答案 1 :(得分:0)

可以使用addMultiOptions()函数

将选择框的选项添加为数组
$persons = new Application_Model_DbTable_Persons();
$data_persons = $persons->findPersons();
$data_persons_array = array();

foreach($this->dataPerson as $datPers)
    $data_persons_array[$datPers->id] =  $datPers->name


$name = new Zend_Form_Element_Text('name');
$name->setLabel('name')
        ->setRequired(true)
        ->addValidator('NotEmpty');
        ->addMultiOptions($data_persons_array); //Will add the options to select box