Howto Create A Dropdownlist?


Could someone give me an example how to create a Dropdownlist please?

I do not understand the help files as there are no examples.

If I have 2 tables, categories and Posts.

I have set categories @has many@ Posts in mysql benchmark.

I would like to output my dropdownlist on the categories_id field

<?= $form->field($model, ‘categories_id’)->textInput() ?>

I tried this:

<?= $form->field($model, ‘categories_id’)->dropDownList() ?> but it did not work.

Thank you,


You need to provide the data items for a dropdown list to work

$items = [1=>'Category 1', 2=>'Category 2'];  // you can generate this from your db table using ArrayHelper

echo $form->field($model, 'categories_id')->dropDownList($items);

You may want to go through the documentation in detail.