CHtml::dropDownList - Select Query From Database | YII

Generate DropdownList from database .

In order to create a dropdownList with data from db, you have to combine it with listData method that will format models from db into array of $key=>$value.

This part is equal to listData Example :
// retrieve the models from db
$models = categories::model()->findAll(
                 array('order' => 'category_name'));
// format models as $key=>$value with listData
$list = CHtml::listData($models, 
                'category_id', 'category_name');
Now, we generate our dropdowList from the $list variable
<?php echo CHtml::dropDownList('categories', $category, 
              array('empty' => '(Select a category'));

Tidak ada komentar

Diberdayakan oleh Blogger.