Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:48:46+00:00 2026-06-14T10:48:46+00:00

The Situation: I’m having some trouble specifying a ‘selected item’ in a dropDownList object

  • 0

The Situation:
I’m having some trouble specifying a ‘selected item’ in a dropDownList object. I’m using a grouped dropDownList having a number of optgroup items which each contain a number of select items. The challenge is trying to find a way to specify the default selected item, because, as mentioned, each item is within an optgroup.

The Question:
How do you set the default selected item when using a grouped form? or Why does the below code not work?

Some Code:

<?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>
//...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:48:47+00:00Added an answer on June 14, 2026 at 10:48 am

    Solved.

    Just needed to set $model->idCategory before calling the dropDownList code. Like so:

    //...
    <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>
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation: I have some persons with certain skills and they can/might belong to more
Situation: I want to provide a website service where users can enter some data
Situation in short. For some reason reverse() method does not work. in PROJECT urls.py
Situation I'm creating a C#/WPF 4 application using a SQL Compact Edition database as
Situation: I need to pass an Objective-C object to an asynchronous C API as
Situation: I'm using both OpenFeint/Game Center in a cocos2d game. OpenFeint has a 'dashboard'
Situation: I am trying to write a efficient query using "LIKE" statement to look
Situation: I have a panorama control with 2 panorama items. First item contain the
Situation is that level of log4net is configured as 'Error' but there is some
Situation: I need a tiny button, with some text on it. Problem: The button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.