Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Yii分组下拉列表中,如何设置默认选中的项目?_Php_Html_Drop Down Menu_Yii - Fatal编程技术网

Php 在Yii分组下拉列表中,如何设置默认选中的项目?

Php 在Yii分组下拉列表中,如何设置默认选中的项目?,php,html,drop-down-menu,yii,Php,Html,Drop Down Menu,Yii,情况: 在中指定“选定项”时遇到问题。我使用的是一个分组的下拉列表,其中有许多optgroup项,每个项都包含许多select项。挑战是试图找到一种方法来指定默认的选定项,因为如前所述,每个项都在一个optgroup中 问题: 使用分组表单时,如何设置默认的选定项?或者为什么下面的代码不起作用 一些代码: <?php // retrieve the parent categories first $parents = Categories::model()->findAllByAtt

情况:
在中指定“选定项”时遇到问题。我使用的是一个分组的下拉列表,其中有许多optgroup项,每个项都包含许多select项。挑战是试图找到一种方法来指定默认的选定项,因为如前所述,每个项都在一个optgroup中

问题:
使用分组表单时,如何设置默认的选定项?或者为什么下面的代码不起作用

一些代码:

<?php
// retrieve the parent categories first
$parents = Categories::model()->findAllByAttributes(array('idCategoryParent'=>0));
// prepare an array to hold parent categories as optgroups, and their child categories as selectable items
$categories = array("Select a Category Below");
// prepare an array to hold the location of the selected item (if any)
$selectedItemInfo = array();


foreach($parents as $parent) {

    // retrieve the sub categories for this parent category
    $children = Categories::model()->cache(Yii::app()->findAllByAttributes(array('idCategoryParent'=>$parent->idCategory));

    // prepare an array to temporarily hold all sub categories for this parent category
    $categoriesChildren = array();

    // iterate over sub categories
    foreach($children as $child) {

        // Assign the category name (label) to its numeric id
        // Example. '10' => 'Coffee Mugs'
        $categoriesChildren[$child->idCategory] = $child->label;

        /**** Important part
        * we may on occasion have an 'id' as a url parameter
        * if 'id' isset, and 
        * if the current sub category idCategory matches 'id'
        * mark this sub category as the item we want selected in our dropDownList
        */
        if(isset($_GET['id']) && $child->idCategory == $_GET['id']){
            // store the location of the current sub category
            $selectedItemInfo[0] = $parent->label;
            $selectedItemInfo[1] = $child->idCategory;
        }
    }

    /*** Another Important part 
    * here we assign the whole child categories array 
    * as a single element in our categories array
    * that can be accessed by the category name (label)
    * of the parent category. 
    * Passing this entire $categories array to our
    * dropDownList will make it render as a grouped
    * dropDownList with the groups as optgroup items.
    */
    // Example. 'Kitchenware' => {array}
    $categories[$parentLabel] = $categoriesChildren;
}

?>

<?php

// get our selected item
/**** Remember
* The element $selectedItemInfo[0] holds our parent category's string name (label)
* The element $selectedItemInfo[1] holds our child category's id
*/
$selectedItem = $categories[$selectedItemInfo[0]][$selectedItemInfo[1]];

// create an options array to inform dropDownList of our selected item
$options = array('options'=>array($selectedItem=>array('selected'=>true)));

?>

//...
<div class="row">
<?php echo $form->labelEx($model,'idCategory'); ?>
<?php echo $form->dropDownList($model,'idCategory', $categories, $options); ?>
<?php echo $form->error($model,'idCategory'); ?>
</div>
//...

已解决。

在调用dropDownList代码之前,只需要设置$model->idCategory。像这样:

//...
<div class="row">
<?php echo $form->labelEx($model,'idCategory'); ?>
<?php $model->idCategory = $_GET['id']; ?>
<?php echo $form->dropDownList($model,'idCategory', $categories); ?>
<?php echo $form->error($model,'idCategory'); ?>
</div>
//...
/。。。
//...

查看CHtml类,特别是listData和listOptions方法。它们是为这类事情设计的。