An Auto Filled Drop Down Box

This is a simple autofilling dropdown.This will give you a good path to do it.

First create a database called ‘data’.

Then we need two tables called ‘city’ and ‘country’.

Use below codes to create ‘city’ table in database called ‘data’.

CREATE TABLE city (

id int(10) unsigned NOT NULL AUTO_INCREMENT,

city varchar(255) DEFAULT NULL,

country_id int(11) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

Then create ‘country’ table in the datasase called ‘data’ using below codes.

CREATE TABLE country (

id int(10) unsigned NOT NULL AUTO_INCREMENT,

country varchar(255) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

Now what we need is to add some data in to our two tables.First I will add data to country table.

INSERT INTO country VALUES (‘1’, ‘SriLanka’);

INSERT INTO country VALUES (‘2’, ‘India’);

INSERT INTO country VALUES (‘3’, ‘Pakisthan’);

Then add data into city table using following set of codes.

INSERT INTO city VALUES (‘1’, ‘Colombo’, ‘1’);

INSERT INTO city VALUES (‘2’, ‘Galle’, ‘1’);

INSERT INTO city VALUES (‘3’, ‘Kandy’, ‘1’);

INSERT INTO city VALUES (‘4’, ‘Dhilli’, ‘2’);

INSERT INTO city VALUES (‘5’, ‘Kalkata’, ‘2’);

INSERT INTO city VALUES (‘6’, ‘Mumbai’, ‘2’);

INSERT INTO city VALUES (‘7’, ‘Islamabadh’, ‘3’);

INSERT INTO city VALUES (‘8’, ‘Abbas’, ‘3’);

INSERT INTO city VALUES (‘9’, ‘Mahanas’, ‘3’);

Now please config the database file in main.php

Now it is the time to create MVC in yii.

Save below codes as DynamicSelectController.php in your controller directory.

<?php

class DynamicSelectController extends Controller

{

public function actionIndex()


{


	&#036;model = new Country();


	&#036;this-&gt;render('index', array('model'=&gt;&#036;model));


}





public function actionFindcity()


{


	&#036;data = City::model()-&gt;findAll('country_id=:country',


			array(':country'=&gt;&#036;_POST['country']));


	&#036;data=CHtml::listData(&#036;data,'id','city');





	echo &quot;&lt;option value=''&gt;City&lt;/option&gt;&quot;;


	foreach(&#036;data as &#036;value=&gt;&#036;name)


		echo CHtml::tag('option', array('value'=&gt;&#036;value),CHtml::encode(&#036;name),true);


}

}

?>

Now I need my two models.One is ‘Country’ and other oen is ‘City’.

First I will create Country model.Save below codes as Country.php in your model directory.

<?php

/**

  • This is the model class for table "country".

  • The followings are the available columns in table ‘country’:

  • @property string $id

  • @property string $country

*/

class Country extends CActiveRecord

{

/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'country';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('country', 'length', 'max'=&gt;255),


		// The following rule is used by search().


		// @todo Please remove those attributes that should not be searched.


		array('id, country', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'country' =&gt; 'Country',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 *


 * Typical usecase:


 * - Initialize the model fields with values from filter form.


 * - Execute this method to get CActiveDataProvider instance which will filter


 * models according to data in model fields.


 * - Pass data provider to CGridView, CListView or any similar widget.


 *


 * @return CActiveDataProvider the data provider that can return the models


 * based on the search/filter conditions.


 */


public function search()


{


	// @todo Please modify the following code to remove attributes that should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id,true);


	&#036;criteria-&gt;compare('country',&#036;this-&gt;country,true);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}





/**


 * Returns the static model of the specified AR class.


 * Please note that you should have this exact method in all your CActiveRecord descendants&#33;


 * @param string &#036;className active record class name.


 * @return Country the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}

}

?>

Now create city model using below codes and save it as City.php in your model dirctory.

<?php

/**

  • This is the model class for table "city".

  • The followings are the available columns in table ‘city’:

  • @property string $id

  • @property string $city

  • @property integer $country_id

*/

class City extends CActiveRecord

{

/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'city';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('country_id', 'numerical', 'integerOnly'=&gt;true),


		array('city', 'length', 'max'=&gt;255),


		// The following rule is used by search().


		// @todo Please remove those attributes that should not be searched.


		array('id, city, country_id', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'city' =&gt; 'City',


		'country_id' =&gt; 'Country',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 *


 * Typical usecase:


 * - Initialize the model fields with values from filter form.


 * - Execute this method to get CActiveDataProvider instance which will filter


 * models according to data in model fields.


 * - Pass data provider to CGridView, CListView or any similar widget.


 *


 * @return CActiveDataProvider the data provider that can return the models


 * based on the search/filter conditions.


 */


public function search()


{


	// @todo Please modify the following code to remove attributes that should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id,true);


	&#036;criteria-&gt;compare('city',&#036;this-&gt;city,true);


	&#036;criteria-&gt;compare('country_id',&#036;this-&gt;country_id);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}





/**


 * Returns the static model of the specified AR class.


 * Please note that you should have this exact method in all your CActiveRecord descendants&#33;


 * @param string &#036;className active record class name.


 * @return City the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}

}

?>

Now what I need is a view to show this.

Create a folder called ‘DynamicSelect’ in your view directory and save the below codes as index.php in it.

<?php

/* @var $this DynamicSelectController */

$this->breadcrumbs=array(

'Dynamic Select',

);

?>

<h1>My life is PHP </h1>

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'regisForm',


'enableAjaxValidation'=&gt;false,

)); ?>

<?php echo $form->dropDownList($model,‘country’,CHtml::listData(Country::model()->findAll(

array(‘order’ => ‘id’)),‘id’,‘country’),

array(

'prompt'=&gt;'Choose your country',


'ajax'=&gt;array(


  'type'=&gt;'POST',


  'url' =&gt; CController::createUrl('findcity'),


  'data'=&gt; array('country'=&gt;'js:this.value'),


  'update'=&gt;'#city_name',))

);

?>

<?php echo CHtml::dropDownList(‘city_name’,’’, array(),

	array('prompt'=&gt;'Choose city')); ?&gt;

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

That is all.

Go to the browser and type this…

‘Enter the root directory of your yii project’/index.php?r=DynamicSelect

you will see the output…