2010-01-29 67 views
0

我如何使用Zend_Form來做到這一點?如何使用Zend_Form創建從數據庫填充的複選框樹?

<ul> 
<?php foreach ($this->roles as $module => $resources): ?> 
    <li> 
     <?php echo $module; ?> 
     <ul> 
      <?php foreach ($resources as $resource => $privileges): ?> 
      <li> 
       <?php echo $resource; ?> 
       <ul> 
        <?php foreach ($privileges as $id => $privilege): ?> 
        <li><input type="checkbox" name="privileges[]" value="<?php echo $id ?>" /><?php echo $privilege; ?></li> 
        <?php endforeach; ?> 
       </ul> 
      </li> 
      <?php endforeach; ?> 
     </ul> 
    </li> 

<?php endforeach; ?> 
</ul> 

我已經開始用類似的東西:

class Admin_Form_AclRole extends Zend_Form { 
    public function __construct($options = NULL) 
    { 
     parent::__construct($options); 
     $this->setName('role'); 




     $role = new Zend_Form_Element_Text('role'); 
     $role->setLabel('Grupo:') 
     ->addFilter('stripTags') 
     ->addFilter('StringTrim') 
     ->setAllowEmpty(false) 
     ->setRequired(true); 



     $privileges = new Zend_Form_Element_MultiCheckbox('foo', array(
     'multiOptions' => array(
      'foo' => 'Foo Option', 
      'bar' => 'Bar Option', 
      'baz' => 'Baz Option', 
      'bat' => 'Bat Option' 
      ) 
     )); 
     $privileges->addDecorator('HtmlTag', array('tag' => 'ul')); 


     $submit = new Zend_Form_Element_Submit('submit'); 
     $submit->setLabel('Cadastrar'); 

     $this->addElements(array($role,$privileges,$submit)); 

    } 
} 

回答

0
$options = $db->fetchPairs(
    $db->select()->from('groups', array('id', 'caption')) 
    ->where('id = ? ',$id)->order('caption ASC'), 'id'); 

    $groups = new Zend_Form_Element_MultiCheckbox('groups'); 

    $groups->addMultiOptions($options); 

    $groups->setLabel('Choose all Groups:'); 

///使用僅如果u希望把條件從db ///中檢索表格的名稱是:組

相關問題