【问题标题】:addElement foreach ZENDaddElement foreach ZEND
【发布时间】:2014-05-19 22:34:48
【问题描述】:

早上好,尝试为数据库创建一个动态选择值,但是可以添加一个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'),
                                 )
                         );

【问题讨论】:

  • 'multiOptions' => $nombre 而不是'multiOptions' => array($nombre) 这就是为什么你得到0
  • 完美,效果很好,非常感谢大家!

标签: php zend-framework


【解决方案1】:

完全可以。

        $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

【讨论】:

  • 谢谢,..并编辑它,你可以检查它,但选择给我一个记录'0'
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 2012-08-30
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多