Zend Framework 1.9从数据库查询中填充复选框

时间:2014-07-15 08:07:58

标签: php arrays database zend-framework checkbox

使用Zend framework 1.9。我在db中有一个包含许多列的表。我只获取某些列:

$select = $table->select();
$select->from($table, array('skill_id', 'description'))->where('parent_skill IS NULL');
$rows = $table->fetchAll($select);

print_r($rows->toArray());

//This is the output :
Array ( [0] => Array ( [skill_id] => 1 [description] => Soccorso stradale ) [1] => Array ( [skill_id] => 4 [description] => Carrozziere ))

现在我要填充一个复选框元素:

$form->skills->setMultiOptions ( $rows->toArray () );

我的问题是方法setMultiOptions期望数组array(key=>value)。 可以将我的$rows数组转换为格式良好的数组吗?

3 个答案:

答案 0 :(得分:1)

尝试为这样的选项构造数组:

$row_options = array();
foreach($rows->toArray() as $key => $val){
    $row_options[$val['skill_id']] =  $val['description'];
}

$form->skills->setMultiOptions ($row_options);

答案 1 :(得分:0)

嗯,是的,我可以猜测

    foreach($rows->toArray() as $subArray){
        foreach($subArray as $val){
            $newArray[] = $val;
        }
    } 
print_r($newArray);die;

答案 2 :(得分:-1)

您可以使用 fetchPair

...

$ select-> from($ table,array(' skill_id',' description')) - > where(' parent_skill IS NULL') ;

$ rows = $ table-> fetchPair ($ select);

$ form-> skills-> setMultiOptions($ row_options);