dependent dropdown box with Yii

2019-05-31 16:54发布

问题:

i have a countries, states, cities tables , and I need 3 dropdown menus wherein, if i select a country like e.g United States, the states dropdown menu will automatically show only the states that are under United States, and next, if I select a State like e.g California, the Cities drop down menu will only show off the Cities under California

am currently having problem implementing this in yii. I have this _form file where these 3 dropdown should be included. I have a partial code, but I can't figure out how to make this work out

this is from the Controller

public function actionDynamicstates()
{
  $sql = "SELECT StateName FROM gg_t_worldareasstates ".
         "WHERE CountryID = :countryid";
  $command = Yii::app()->createCommand($sql);
  $command->bindValue(':countryid', $_POST['CountryID'], PDO::PARAM_INT);
  $data = $command->execute();

  $data = CHtml::listData($data,'StateID','StateName');
  foreach($data as $value=>$name)
  {
    echo CHtml::tag('option',
    array('value'=>$value),CHtml::encode($name),true);
  }
}

this is from the _form view file

<div class="row">
<?php 
$country = new CDbCriteria; 
$country->order = 'CountryName ASC';
echo $form->dropDownList($model, 'CountryID', 
  CHtml::listData
  (
    Worldareascountries::model()->findAll($country),
    'CountryID',
    array
    (
      'ajax' => array
      (
        'type' => 'POST',
        'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
        'update' => '#StateID',
      )
    )
  )
);
echo $form->dropDownList('StateID','', array());
?>
</div>

I changed that those codes above into this one

    <div class="row">
    <?php echo $form->labelEx($model,'Country'); ?>
    <?php 
          $country = new CDbCriteria; 
          $country->order = 'CountryName ASC';
    ?>
    <?php 
          echo $form->dropDownList($model,'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID','CountryName'),
                    array(
                        'ajax' => array(
                        'type' => 'POST',
                        'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
                        'update' => "#StateID"
                    )       
              )
          );
    ?>
    <?php echo $form->error($model,'CountryID'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'State'); ?>
    <?php 
          $state = new CDbCriteria;
          $state->order = 'StateName ASC';
    ?>
    <?php 
          echo $form->dropDownList($model,'StateID',CHtml::listData(Worldareasstates::model()->findAll($state),'StateID','StateName'),
                    array(
                        'ajax' => array(
                        'type' => 'POST',
                        'url' => CController::createUrl('wsmembersdetails/dynamiccities'),
                        'update' => '#CityID'
                    )   
                )
          );
    ?>
    <?php echo $form->error($model,'StateID'); ?>
</div>


<div class="row">

    <?php echo $form->labelEx($model,'CityID'); ?>
     <?php echo $form->dropDownList($model,'CityID','',array());?>
    <?php echo $form->error($model,'CityID'); ?>

回答1:

change this line
$data = $command->execute();
to
$data = $command->query();



回答2:

this problem was solved, it's on the yii wiki docs