Display states in dropdown

(chinaka) #1

Hello Guys,

The code below is responsible for listing all the state name i have in my database through a dropdown.

Normally in gridview to list the states i will ave something like this:

‘attribute’ => ‘state_id’,
‘value’ => function ($model) {
return $model->state->name;
‘filter’ => ArrayHelper::map(States::find()->asArray()->all(), ‘ID’, ‘name’),

But this time around i want to display the states outside gridview using the code below so where we have array below we will have the states instead, please how do i achieve this?

<div class=“col-md-2”>

<?= $form->field($model, ‘state_id’)->label(‘State’)->dropDownList(

array(1 => “state1”, 2 => “state2”, 3 => “state3”, 7 => “state4”, 8 => “state5”, 9 => “state6”)



(Mehdi Achour) #2

Does this work for you?

<?= $form->field($model, ‘state_id’)->label(‘State’)->dropDownList(

ArrayHelper::map(States::find()->asArray()->all(), ‘ID’, ‘name’)


(chinaka) #3

Yes works just find but i used this instead:

ArrayHelper::map(\app\models\States::find()->all(), ‘ID’, ‘name’),