N
N
nnkrasovok2019-05-07 20:57:49
Yii
nnkrasovok, 2019-05-07 20:57:49

How to display data from a table using dropdown?

There is such a structure of the subcategory table: id, category_id, name (that is, the ID, to which ID of the category the name of the subcategory belongs).
How can I get the list from the database like this?
5cd1c6981ffd2589344908.jpeg
i.e. a grouped list: category first, below it subcategories related to it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-05-07
@nnkrasovok

Good evening.
You need to collect a multidimensional array, which you can pass to dropDownList().
It looks like this:

$form->field($model, 'status')->dropDownList(
        [
            'Активный' => ['Админ', 'Модератор', 'Пользователь'],
            'Отключён' => ['За нарушения', 'Самостоятельно']
        ],
        [
            'prompt' => 'Выбрать статус'
        ])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question