Reputation: 18346
I am new to Yii framework.
I have a form with three fields. I need one of those be a select drop down element that its data comes from previously added data which are in mysql table.
How can I do it ?
Upvotes: 0
Views: 8741
Reputation: 4150
If you have a model set up for the table that contains the data you want to use in your dropdown list you can use the CHtml::dropDownList()
method the render a dropdown list, and CHtml::listData()
to render that model into items for the list, for example;
echo CHtml::dropDownList(
'attribute_name',
'',
CHtml::listData(MyOtherModel::model()->findAll(),'id','name')
);
I use Gii a lot, which uses CActiveForm widget to display forms, if your form uses CActiveForm too you could render your dropdown something like;
$form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
));
...
echo $form->label($model,'attribute_name');
echo $form->dropDownList(
$model,
'attribute_name',
CHtml::listData(MyOtherModel::model()->findAll(),'id','name')
);
...
$this->endWidget();
Note that CActiveForm uses CHtml::activeDropDownList()
rather than CHtml::dropDownList()
that I used in my first example, hence the slight difference in syntax between my two examples.
Upvotes: 1