如何使用zend 1.11中的表单在视图中显示数据库中读取数据的复选框

时间:2015-09-16 07:05:10

标签: php forms zend-framework checkbox zend-form

从数据库中读取表单数据,并使用zend 1.11中的表单在视图中显示复选框

  1. 我在zend controller

    中创建了一个roleController.php
    <?php
    
    class RoleController extends Zend_Controller_Action
    {
    
        public function init()
        {
            /* Initialize action controller here */
        // do some thing    
    
        }
        public function indexAction()
        {
            // action body
        // do some thing    
        }               
    }            
    ?>
    
  2. 我还在zend 1.11

    中的表单文件夹中创建了role.php
    <?php
    
    class Application_Form_Setting extends Zend_Form
    {
    
        public function init()
        {
            /* Form Elements & Other Definitions Here ... */
    
            $this->setMethod('post');
    
    
            $this->addElement('checkbox', 'role[]', array(
                'filters'    => array('StringTrim')
            ));
    
    
            $this->addElement('button', 'Save', array(
                'value'    => 'Submit',
                'type'     => 'Submit',
                'class'    => 'btn blue'
            ));
            // Add the submit button
    
    
        $this->role[]->removeDecorator('Label');
            $this->role[]->removeDecorator('HtmlTag');
        $this->Save->removeDecorator('Label');
            $this->Save->removeDecorator('HtmlTag');
            $this->Save->removeDecorator('DtDdWrapper');
        }
    
    
    }
    
    ?>
    

    role.php在模型中的db-table文件夹中

    <?php
    
    class Application_Model_DbTable_Role extends Zend_Db_Table_Abstract
    {
    
        protected $_name = 'role_tbl';
        protected $_primary  = 'role_id';
    
    
    }
    
    <\ n> 查看role.phtml

    <label><?php 
    echo $this->form->getElement('role[]'); ?>Index</label>
    

    我的问题是,如何使用表单从数据库中读取数据并显示复选框

    enter image description here

    我的role_table是

    +-----------------------+------------------+
    |   role_id             | role_name        | 
    +-----------------------+------------------+
    |      1                | Admin            |
    |      2                | Manager          |
    |      3                | Marketing Persion|
    |      4                | Employee         |
    +-----------------------+------------------+
    

1 个答案:

答案 0 :(得分:0)

检查以下代码。

class YourForm extends Zend_Form
{
    public function init()
    {

       $DBCnt = getCountofRowsFromDB();

       for($i = 0; $i <= $DBCnt; $i++){
           $checkBoxes[] = new Zend_Form_Element_Checkbox('checkBox_' . $i);//THIS IS WHERE YOU CREATE AND SET THE CHECKBOXES
       }
       $this->addElements($checkBoxes); //THIS IS WHERE YOU ADD THE CHECKBOXES TO THE FORM
  }
}