添加元素foreach ZEND

时间:2014-05-19 22:34:49

标签: php zend-framework

早上好,尝试为数据库创建一个动态选择值,但是你可以添加一个foreach这个选择吗?

$table = new Application_Model_DbTable_Artista();

$nombre = array();

        foreach ($table->listar() as $c)
        {    
            $id = $c->id;
            $nombre[$id] = $c->nombre;

        }


$this->addElement('select','artista_id',
                          array(
                                 'value' => 'white',
                                 'multiOptions' => array($nombre
                                                         ),
                                 'decorators'   =>  array('ViewHelper', 'Errors'),
                                 )
                         );

1 个答案:

答案 0 :(得分:0)

完全可以。

        $roles = $roleTable->fetchAll();
        $selRol = array();
        foreach($roles as $role) {
            $id = $role->getId();
            $selRol[$id] = $role->getName();
        }

        $this->setAttribute('method', 'post');
        $this->add(array(
            'name' => 'username',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Usuario',
            ),
        ));
        $this->add(array(
            'name' => 'password',
            'attributes' => array(
                'type'  => 'password',
            ),
            'options' => array(
                'label' => 'Clave',
            ),
        ));
        $this->add(array(
            'name' => 'email',
            'attributes' => array(
                'type'  => 'email',
            ),
            'options' => array(
                'label' => 'Email',
            ),
        ));
        $this->add(array(
            'type' => 'Zend\Form\Element\Select',
            'name' => 'role_id',
            'options' => array(
                'label' => 'Role',
                'value_options' => $selRol,
             ),
        ));

来源:https://github.com/FraGoTe/admin-dashboard-zend/blob/master/module/Dashboard/src/Dashboard/Form/UserForm.php