dropDownList array Problem

Hello guys. I want a drop down list, the data in it will come from table. Now solved this problem with following approach.

$contacts = array('' => 'Select Contact');

foreach (Contact::model()->findAll() as $row)


    $contacts += array($row->id => $row->name);


echo $form->dropDownList($model, 'contact_id', $contacts, array('style' => 'width:210px'));

But this is not appropriate approach. But i also tried below which produce errors :(

echo $form->dropDownList($model, 'contact_id', Contact::model()->findAll(), array('style' => 'width:210px'));

what is the exact approach for displaying a table data in drop down list?


Try using this approach using the CHtml helper class:

echo $form->dropDownList($model, 'contact_id', CHtml::listdata(Contact::model()->findAll(),'id','name'),array('style' => 'width:210px'))

This basically creates an associative array of your contacts

Solved :) thanx dudeā€¦