Skip to content

Instantly share code, notes, and snippets.

@StandardNerd
Created June 11, 2012 08:41
Show Gist options
  • Save StandardNerd/2909111 to your computer and use it in GitHub Desktop.
Save StandardNerd/2909111 to your computer and use it in GitHub Desktop.
yii DropDownList with option groups
Example 5: Generating dropdownlist with option groups.
from http://www.yiiframework.com/wiki/48/by-example-chtml/#hh4
If you need to generate dropdownlist using both optgroup and option tags use the following code.
<div class="cars-select">
<?php echo CHtml::dropDownList('Cars', 'car_id', array(
'Mazda'=>array(
'mazda-rx7'=>'RX7',
'mazda-rx5'=>'RX5',
),
'Volvo'=>array(
'volvo-b9tl'=>'B9TL',
'volvo-l90e-radlader'=>'L90E Radlader',
),
)); ?>
</div>
But i get the data from the Database with following table-structure:
Table Studycourse:
id, coursename, extension_of
1, Law, 0
2, Economics, 0
3, Business Law, 1
4, Macro Economics, 2
How can i populate a dropdownlist with option groups?
It should generate the following html:
<select name="studycourse" id="course">
<optgroup label="Law">
<option value="law-business">Business Law</option>
</optgroup>
<optgroup label="Economics">
<option value="economics-macro">Macro Economics</option>
</optgroup>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment