使用TbActiveForm选择列表中的动态选项

时间:2015-08-19 06:37:32

标签: php yii

我在控制器中有以下代码,

$model=new Guessgame('search');
$model->unsetAttributes();  // clear any default values
if(isset($_GET['Guessgame']))
    $model->attributes=$_GET['Guessgame'];
    $this->render('admin',array(
        'model'=>$model,
));

在视图文件中,

<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'=>'project1-form',
'enableAjaxValidation'=>false,

'htmlOptions' => array('enctype' => 'multipart/form-data','class' => 'well'),
'type'                   => 'horizontal',
     'enableAjaxValidation'   => false,
    'enableClientValidation' => true,
    'clientOptions'          => array(
        'validateOnSubmit' => true,
    )
)); ?>
<p class="help-block">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>
<?php echo $form->dropDownListRow($model,'type',array('logo'=>'Logo','apaters'=>'Apaters','text'=>'Text'),array('class'=>'span5','maxlength'=>255)); ?>

上面的示例列表项是静态的(徽标,分隔符和文本。

但我想从数据库中获取动态值。请帮帮我。

1 个答案:

答案 0 :(得分:0)

您可以在模型中编写以下函数来从db

获取值
function getValues(){

    $crit = new CDbCriteria();
    $crit->select = 'name';
    $crit->order = 'name';
    $data = YourModel::model()->findAll($crit);
    $result = CHtml::listData($data,'id','name');
    return $result;

}

在视图中

<?php 
    echo $form->dropDownListRow($model, 'type', YourModel::model()->getValues(), array('class'=>'span5', 'maxlength'=>255)); 
?>

希望这样可以解决您的问题