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.

http://www.yiiframework.com/doc/api/1.1/CHtml#listData-detail

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:

[list=1]

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

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

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

[/list]

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

Hi,

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

F:\xampp\htdocs\testdrive\protected\views\user\_form.php(50)

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>

46

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>

54

55 <div class="row buttons">

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

57 </div>

58

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

60

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

try


print_r($cityOptions); exit;

is there any data in city table?

Ouch, I’m sorry.

ankur,

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(

		array('order'=>'name'));

	$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); ?>

with


<?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('city.name',$this->city_grid, true);

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


'sort'=>array(

	'attributes'=>array(

		'city_grid'=>array(

			'asc'=>'city.name',

			'desc'=>'city.name DESC',

		),

		'*',

	),

),

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


'cityId',

with the following:


array(

	'name'=>'city_grid',

	'filter'=>User::model()->getCityOptions(),

	'value'=>'$data->city->name',

),

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:


array(

	'name'=>'city_grid',

	'value'=>CHtml::encode($model->city->name),

),

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).