F
F
funtik522016-03-24 05:32:46
Yii
funtik52, 2016-03-24 05:32:46

How to correctly take data from the model thanks to "listdata" and "activeDropDownList"?

Hello. I'm building a book learning website. It is necessary to create a sub-category (an entry in the MySQL database table), the category of which is selected from the drop-down list. Here's the incomplete form:

<h1><?php echo $title ?></h1>
<?php echo CHtml::beginForm(); ?>
<div class="label"><?php echo CHtml::activeLabelEx($subcategory,
"category") ?></div>
<?php $listData = CHtml::listData($categories, "id", "name") ?>
<div class="control"><?php echo CHtml::activeDropDownList($subcategory,
"category", $listData) ?></div>
<?php echo CHtml::error($subcategory, "category") ?>

. . .

<div class="button"><?php echo CHtml::submitButton("Сохранить"); ?></div>
<?php echo CHtml::endForm() ?>
<p>&nbsp;</p>
<p><?php echo CHtml::link("Назад", array("subcategory/index")) ?></p>

It turns out that with the correct generation of the list:
<?php $listData = CHtml::listData($categories, "id", "name") ?>
<div class="control"><?php echo CHtml::activeDropDownList($subcategory,
"category", $listData) ?></div>

elements do not have names. The funny thing is that if you write "id", "id" (, ), then the names appear. However, all sub-category entries have a category of "0". And must have a value corresponding to the "id" of the element selected from the list. Like this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bay, 2016-03-24
@funtik52

Do you have a name field in $categories? do var_dump first, see what is stored there in principle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question