Php 使用TbActiveForm选择列表中的动态选项
我在控制器中有以下代码Php 使用TbActiveForm选择列表中的动态选项,php,yii,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, )); 在视
$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)); ?>
带*的字段是必需的
上面的示例中,列表项是静态的(logo、aparter和text)
但我需要数据库中的动态值。请帮助我。您可以在模型中编写以下函数以从数据库中获取值
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));
?>
希望这能解决你的问题