I have three tables and i have created a dropdownlist for the country. I want it that when the dropdown is clicked and a country is selected, I want it that all the states and cities relating to the country will be displayed. Please how do I do this. I have these three tables:
–
– Table structure for table country
–
CREATE TABLE country
(
country_id
int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
country_name
varchar(35) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
–
– Table structure for table state
–
CREATE TABLE state
(
state_id
int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
state_name
varchar(35) NOT NULL,
state_country_id
int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE city
(
city_id
int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
city_name
varchar(35) NOT NULL,
city_state_id
int(11) NOT NULL,
city_country_id
int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Country Model:
public function attributeLabels()
{
return [
'country_id' => Yii::t('app', 'Country ID'),
'country_name' => Yii::t('app', 'Country Name'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCities()
{
return $this->hasMany(City::className(), ['city_country_id' => 'country_id']);
}
public function getStates()
{
return $this->hasMany(State::className(), ['state_country_id' => 'country_id']);
}
public static function getAllCountry()
{
$dataTmp = self::find()->where(['is_status' => 0])->orderBy('country_name')->all();
$result = yii\helpers\ArrayHelper::map($dataTmp, 'country_id', 'country_name');
return $result;
}
See my country view below. I stopped there because I dont know what to do again. Please help me.
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\bootstrap\Modal;
/* @var $this yii\web\View */
/* @var $searchModel app\models\CountrySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Country List');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Configuration'), 'url' => ['default/index']];
?>
<div class="col-md-12">
<!-- <div class="country-list-form">-->
<?php $form = ActiveForm::begin([
'id' => 'country-list-form',
'fieldConfig' => [
'template' => "{label}{input}{error}",
],
]); ?>
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title"><i class="fa fa-user-cap"></i> <?php echo Yii::t('app', 'Country: Based on City & State'); ?></h3>
<div class="box-tools <?= (Yii::$app->language == 'ar') ? 'pull-left' : 'pull-right'; ?>">
<button class="btn btn-success btn-sm" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
<div class="box-body">
<?= $form->field($model, 'country_id')->dropDownList(ArrayHelper::map(app\models\country::find()->all(),'country_id','country_name'),['prompt'=>Yii::t('app', '--- Select Inspection Country ---')])->label(false); ?>
</div>
<?php
?>
<div class="box-body table-responsive no-padding">
<?php
// What do I do?
?>
</div>
</div>
<?php ActiveForm::end(); ?>
<!-- </div>-->
</div>
<!--/div> -->