Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend-使用数据库返回的数据填充组合框_Php_Zend Framework_Zend Form_Zend Db_Zend Studio - Fatal编程技术网

Php Zend-使用数据库返回的数据填充组合框

Php Zend-使用数据库返回的数据填充组合框,php,zend-framework,zend-form,zend-db,zend-studio,Php,Zend Framework,Zend Form,Zend Db,Zend Studio,我试着这样做: public function init() { /* Form Elements & Other Definitions Here ... */ $sets_table = new Optionals_Model_DbTable_Sets(); $set = new Zend_Form_Element_Select('set'); $set ->setLabel('Alegeti setul de optionale:');

我试着这样做:

public function init()
{
    /* Form Elements & Other Definitions Here ... */

    $sets_table = new Optionals_Model_DbTable_Sets();

    $set = new Zend_Form_Element_Select('set');
    $set ->setLabel('Alegeti setul de optionale:');

    foreach ($sets_table->getSets() as $value) {
        echo $value->cod_set_optional;
        $set->addMultiOption($value->cod_set_optional);
    }

    $submit = new Zend_Form_Element_Submit('Continua');

    $this->addElements ( array (
            $set,
            $submit
    ) );
}
其中,DbTable中的getSets()如下所示:

public function getSets()
{
    $select = $this->select();
    $rows = $this->fetchAll($select);
    if (!$rows) {
        throw new Exception("Could not find!");
    }
    return $rows;
}
我见过这种做法,但不起作用。echo工作正常,但组合框未填充。我的代码有什么问题吗

谢谢大家!! 索林

addMultiOption($option,$value)

您应该这样做:

 foreach ($sets_table->getSets() as $set) {
        $set->addMultiOption($set->id , $set->value);
    }

确保getset从表中同时返回id和值。

如果使用fetchAll方法只需使用

$list->addMultiOptions(usermodel::getInstance()->getusers());
整个代码是这样的

$list      =   new Zend_Form_Element_Select('users');
$list->setLabel('Select a user')
          ->setRequired(true)
          ->addValidators(array(array('notEmpty',true, array('messages' => array('isEmpty' => 'Please select a User')))));
$list->addMultiOptions(usermodel::getInstance()->getusers());
die($list);
在模型中

class usermodel extends Zend_Db_Table
{
    protected  $_name               =   'users';
    protected  static $_instance    =   null;

    public function getusers()
    {
        $sql          =     "SELECT us.`id` AS `key`,CONCAT(us.`firstname`,' ',us.`lastname`) AS `value` FROM   
     `users` us 
                            ORDER BY us.`firstname` ASC";  
        return  $this->getAdapter()->fetchAll($sql);
    }

    public static function getInstance(){
         if( !isset(self::$_instance) ){
            $instance = new self();
            self::$_instance = $instance;
         }
         return self::$_instance;
    }

}

但我只想将ID添加到comboboxi中,我是这样做的$set->addMultiOption($value->cod\u set\u可选,$value->cod\u set\u可选);因为我只需要代码…谢谢