A common situation would be needing a list of countries displayed in a select list, that are pulled from a database table.

Assuming a model named ‘Countries’ (try generating with Gii), the basic code for the view is the following. It makes use of the findAll() method to query all rows in the country database, and CHtml:listData which generates an array of data suitable for list-based HTML elements and the dropDownList function.

echo $form->dropDownList(
    $model,
    'country_id',
    CHtml::listData(Countries::model()->findAll(),'id','name')
);

Posted in: Yii

Related FAQ's

Marius Ion ANGEL HOT SOFT LLC (800) 316-7677