php active控件,php – 使用TbActiveForm选择列表中的动态选项
我在控制器中有以下代碼,
$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,
)
)); ?>
Fields with * are required.
<?php echo $form->errorSummary($model); ?>
<?php echo $form->dropDownListRow($model,'type',array('logo'=>'Logo','apaters'=>'Apaters','text'=>'Text'),array('class'=>'span5','maxlength'=>255)); ?>
上面的示例列表項(xiàng)是靜態(tài)的(徽標(biāo),分隔符和文本.
但我想從數(shù)據(jù)庫中獲取動(dòng)態(tài)值.請(qǐng)幫我.
解決方法:
你可以在模型中寫下函數(shù)來從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;
}
在視野中
echo $form->dropDownListRow($model, 'type', YourModel::model()->getValues(), array('class'=>'span5', 'maxlength'=>255));
?>
希望這樣可以解決您的問題
標(biāo)簽:php,yii
來源: https://codeday.me/bug/20190711/1433880.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的php active控件,php – 使用TbActiveForm选择列表中的动态选项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ipython怎么安装_ipython的
- 下一篇: php 上次登陆时间,php使用cook