Adding Dropdown List

can anybody help me how to create drop down list in yii all view files?


<?CHtml::dropDownList($model,'coupon_id', CHtml::listData(Coupon::model()->findAll("user_id=$model->user_id"),'id','title')); ?>

i have to fetch distinct of data from database

Tiny correction; you’ll need to use <?= or <? echo to output the drop down list.

where i need to add this code? i am new to yii…, plz help me

in your view file

i hacve added in view/dataform.php like

<div class="row">

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'tier'); ?&gt;

	&lt;?php echo CHtml::dropDownList(&#036;model,'tier', CHtml::listData(Coupon::model()-&gt;findAll(&quot;tier=&#036;model-&gt;tier&quot;),'AssetID','title')); ?&gt;

	&#60;&#33;--&lt;?php echo &#036;form-&gt;;dropDownList(&#036;model,'tier', CHtml::listData(Tier::model()-&gt;findAll(), 'AssetID', 'tier'), array('empty'=&gt;;'--please select--')); ?&gt;--&#62;

	&lt;?php echo &#036;form-&gt;error(&#036;model,'tier'); ?&gt;


still getting text box only…, do i need to add extra code in datacontroller.php??in actionCreate()

You should be using the $form->dropDownList() method in your case. Why do you have a $form->dropDownList() call commented out in your HTML, along with random semicolons?

plz explain in detail

try this


$list =  CHtml::listData(ModelName::model()->findAll(array('group'=>'field u  want to be distinct')), 'save_field', 'display_field');


echo $form->dropDownList($model,'save_field', $list); 


Model will b Ur Model Name from which U want to Fetch Data

save_field is that field which u want to save value of

display_field is that field to display to User in dropdown