Revision #15                                    has been created by  ricardograna                                    on Oct 7, 2009, 7:42:51 PM with the memo:
 ricardograna                                    on Oct 7, 2009, 7:42:51 PM with the memo:
                                
                                
                                    Added listData                                
                                                                    « previous (#14)                                                                                                    next (#16) »                                                            
                            Changes
                            
    Title
    unchanged
    By Example: CHtml
    Category
    unchanged
    Tutorials
    Yii version
    unchanged
    
    Tags
    unchanged
    
    Content
    changed
    [...]
<?php echo CHtml::textField('Text', 'some value', 
    array('disabled'=>true); ?>
```
_  
 
 
_  
 
 
 
<a name="textField"></a>
 
## CHtml::listData() method
 
 
~~~
 
public static function listData($models,$valueField,$textField,$groupField='')
 
~~~
 
Generates data for dropDownList and listBox, using the format $key=>$value.
 
 
 
***
 
 
### Example 1: Generating a list data for categories
 
 
```php 
<?php 
 
     /*you can use here any find method you think 
 
     proper to return your data from db*/
 
     $models = categories::model()->findAll();
 
 
     // format models resulting using listData     
 
     $list = CHtml::listData($models, 
 
                'category_id', 'category_name');    
 
 
     print_r($list);
 
```
 
 
#### HTML Output (Example):
 
 
```php 
array("1" => "Arts", "2" => "Science", "3" => "Culture");
 
```
 
     
 
 
***
 
 
### Example 2: Generating an ordered list data for categories using findAll parameter
 
 
```php 
<?php 
 
     $models = categories::model()->findAll(
 
                 array('order' => 'category_name'));
 
 
     $list = CHtml::listData($models, 
 
                'category_id', 'category_name');    
 
 
     print_r($list);
 
```
 
 
#### HTML Output (Example):
 
 
```php 
array("1" => "Arts", "3" => "Culture", "2" => "Science");
 
```
 
 
_[...]
```
 
***
 
 
### Example 4: Using data from db
 
 
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 2:
 
 
 
```php 
// 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 
<?php echo CHtml::dropDownList('categories', $category, 
 
              $list,
 
              array('empty' => '(Select a category'));
 
```
 
 
***
You cand find CHtml class at yii/framework/web/helpers/CHtml.php