Show City Name In Combo Box

Hi Guys,

I am new in yii. I have city name and value in tbl_city and I want to show city comb box on add user page so that user can choose city and submit. After this i have to save city value in database and on edit user page i have to show city combo and select the city value by default on the basis of city id which is store in table. same i have to show on search section and view section.

Looking forward for response.

Quick response: use giix :)

Please reply on my query…

I’m serious, it can help you by generating the views.

Otherwise, you can do it manually with CActiveForm::dropDownList.

You can use CHtml::listData() method.

If you want to use a dropDownList in your form:

$form = $this->beginWidget('CActiveForm', ... );


echo $form->label($model,'city_id');

$cityOptions = CHtml::listData(City::model()->findAll(), 'id', 'name');

echo $form->dropDownList($model, 'city_id', $cityOpitons, array('prompt' => 'Select your city'));


CHtml::listData() returns an array of value-label pairs. It serves quite well when used in a CActiveForm::dropDownList to generate a list of options.

And when you use CActiveForm::dropDownList, the initial selection of the dropDownList is automatically set according to your model … It should show ‘Select your city’ when you are creating a new model instance, and should show the name of the selected city when you are updating the existing one.

Well, giix should do some kind of magic and automatically generate a form code that is almost similar to the example above … I’m not very sure because I’m not familiar with it. You may try it, it’s one of the most favored extensions.

But I think it’s not too bad to do it manually, especially when you are not accustomed to the concept of relational active record.

Actually this funtion is define in class.

public function dropDownList($model,$attribute,$data,$htmlOptions=array())


	return CHtml::activeDropDownList($model,$attribute,$data,$htmlOptions);


I want to know how can i call this function and fetch the information from database.

Hi ankur,

softark’s post contains all you need to:


[*]fetch the data from the database, with CActiveRecord::findAll,

[*]prepare the data, with CHtml::listData, and finally

[*]show the dropdown, with CActiveForm::dropDownList.


Please check his post for an example on how to use these methods.


I have created a module by gii and used below code on form to display combo box of city.

<?php $cityOptions = CHtml::listData(City::model()->findAll(), ‘id’, ‘name’);

echo $form->dropDownList($model, ‘id’, $cityOpitons, array(‘prompt’ => ‘Select your city’));


but getting below error message.

Undefined variable: cityOpitons


38 <?php echo $form->textField($model,‘vchFName’,array(‘size’=>60,‘maxlength’=>128)); ?>

39 <?php echo $form->error($model,‘vchFName’); ?>

40 </div>

41 <div class="row">

42 <?php echo $form->labelEx($model,‘bintMobileNo’); ?>

43 <?php echo $form->textField($model,‘bintMobileNo’,array(‘size’=>60,‘maxlength’=>128)); ?>

44 <?php echo $form->error($model,‘bintMobileNo’); ?>

45 </div>


47 <div class="row">

48 <?php echo $form->labelEx($model,‘intCityId’); ?>

49 <?php $cityOptions = CHtml::listData(City::model()->findAll(), ‘id’, ‘name’);

50 echo $form->dropDownList($model, ‘id’, $cityOpitons, array(‘prompt’ => ‘Select your city’));

51 ?>

52 <?php echo $form->error($model,‘intCityId’); ?>

53 </div>


55 <div class="row buttons">

56 <?php echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>

57 </div>


59 <?php $this->endWidget(); ?>


61 </div><!-- form -->

Please check the spelling

<?php $cityOptions = CHtml::listData(City::model()->findAll(), ‘id’, ‘name’);

echo $form->dropDownList($model, ‘id’, $cityOpitons, array(‘prompt’ => ‘Select your city’));

Please use any IDE like NetBeans and use its code complete feature to avoid this type of simple mistakes


print_r($cityOptions); exit;

is there any data in city table?

Ouch, I’m sorry.


This is the method I use, borrowed heavily from the Agile Yii development book.

In User model class:

public function getCityOptions()


	$cityModel = City::model()->findAll(


	$cityArray = CHtml::listData($cityModel, 'id','name');

	return $cityArray;


This code does three things:

  • gets a list of data from the city table sorted by name

  • creates an array of id/name value pairs

  • returns that array

Then in your User form:

<?php echo $form->labelEx($model,'cityId'); ?>

<?php echo $form->dropDownList($model,'cityId',$model->getCityOptions()); ?>

<?php echo $form->error($model,'cityId'); ?>

This assumes that your User table has a cityId column to populate from the related table. It should give you a dropdown list that shows the names but will return the id number on submit.

The _search partial uses an ActiveForm also, so _search should be updated the same way.

The _view partial couldn’t be simpler. Just replace the normal

<?php echo CHtml::encode($data->cityId); ?>


<?php echo CHtml::encode($data->city->name); ?>

Most place you have a User model loaded, in fact, you should be able to use similar drill-downs, for example, in the view, you can replace $model->cityId with $model->city->name.

GridViews and views in child models are a little more tricky. For example, in the User model, you can also add city name to the GridView in admin with the following steps:

  • declare a public variable e.g. $city_grid

  • in the rules array, make sure to include city_grid in your list of items safe on search

  • in your attribute labels, make sure to assign a label to city_grid, e.g. ‘City’

  • in your search function, include the following in your criteria:

$criteria->compare('',$this->city_grid, true);

and then when you return the CActiveDataProvider, include after the ‘criteria’ setting of the array:





			'desc'=>' DESC',





If you do all of these things, then in your User admin render, you can replace:


with the following:






That should replace the cityId field in your GridView with the name of the city, including the ability to search and sort on the city name instead of the id.

If you have tables with many:many relations, for example an Image table that you would want to link to User and to City, you will also need to set up a city_grid in your CityImage model, and then in the DetailView of the regular view file, replace the cityId in the attributes array with:





I don’t know if that’s the best way to manage pure relational tables in Yii, but it is simple to set up using Gii, and it works with this modification to refer back to the parent table(s).