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()
{
$model = new Country();
$this->render('index', array('model'=>$model));
}
public function actionFindcity()
{
$data = City::model()->findAll('country_id=:country',
array(':country'=>$_POST['country']));
$data=CHtml::listData($data,'id','city');
echo "<option value=''>City</option>";
foreach($data as $value=>$name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($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'=>255),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, country', 'safe', 'on'=>'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=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'country' => '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.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('country',$this->country,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Country the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($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'=>true),
array('city', 'length', 'max'=>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'=>'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=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'city' => 'City',
'country_id' => '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.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('city',$this->city,true);
$criteria->compare('country_id',$this->country_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return City the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($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'=>'regisForm',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->dropDownList($model,‘country’,CHtml::listData(Country::model()->findAll(
array(‘order’ => ‘id’)),‘id’,‘country’),
array(
'prompt'=>'Choose your country',
'ajax'=>array(
'type'=>'POST',
'url' => CController::createUrl('findcity'),
'data'=> array('country'=>'js:this.value'),
'update'=>'#city_name',))
);
?>
<?php echo CHtml::dropDownList(‘city_name’,’’, array(),
array('prompt'=>'Choose city')); ?>
<?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…