Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Yii - ActiveDropDownList() default selected

Tags:

php

yii

I have the following active drop down list

<?php 
echo CHtml::activeDropDownList($project, 'city', CHtml::listData(City::model()->findAll(), 'id', 'name'), array('class'=>'st-form', 'onchange' => 'getLocationByCity(this)')); 
?>

I want to add selected option to the 10th value in the drop down list when the list is being created, how do I do it.

Thanks.

like image 786
Zaje Avatar asked Apr 15 '12 22:04

Zaje


1 Answers

The drop down list automatically selects the option corresponding to the value of the specified attribute of the specified model. In this case, the option with value equal to $project->city is preselected.

So if you want to control which option that is, simply do

$project->city = $valueOfThatOption;

before calling activeDropDownList.

It doesn't (and shouldn't) matter if that option is 1st, 10th, or anything else.

like image 108
Jon Avatar answered Nov 15 '22 12:11

Jon