如何使用以下代码填充yii中的列表框

时间:2014-04-16 11:31:18

标签: php yii

$selected   = array( '5' => array('selected' => 'selected'),'6' => array('selected' => 'selected'),);
$htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'true', 'options' => $selected);

echo $form->listBox($model,'team_members', $mem_arr, $htmlOptions);

从上面的代码可以正常工作。但我选择变量作为数组变量.. 然后我如何给出而不是'$selected'数组

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你可以这样做(未经测试):

$result = array(5,6);

$selected = array():
foreach ($result as $selectedIndex) {
    $selected[$selectedIndex] = array('selected'=>'selected');
}
$htmlOptions = array(
    'size' => '5',
    'prompt'=>'Use CTRL to Select Multiple Staff',
    'multiple' => 'true',
    'options' => $selected);

echo $form->listBox($model,'team_members', $mem_arr, $htmlOptions);