how to populate a dropdown list with database data

as subject :how to populate a dropdown list with database data

createUser.php

there is a dropdownlist (country)in a form, when this page onload, the dropdownlist has been filled with the country data.

thanks

Use something like this:



	$criteria=new CDbCriteria;


		$criteria->select='AlId,AlDescr';  // only select the 'AlId' and 'AlDescr' columns


		


		$qAlbums=Album::model()->findAll($criteria);


		


		$albums = array();


		foreach($qAlbums as $p)


		{


			$albums[$p->AlId] = $p->AlDescr;


		}


		return $albums;


and then give $albums as parameter to CHtml::activeDropDownList($model, 'attribute', $albums);

There is a better approach. You may use CHTML::listData() for this.



$criteria=new CDbCriteria;


$criteria->select='AlId,AlDescr';  // only select the 'AlId' and 'AlDescr' columns		


$qAlbums=Album::model()->findAll($criteria);


and then



CHtml::activeDropDownList($model, 'AlId', CHtml::listData($model,'AlId','AllDesc')); 


For more details please see my topic where I’ve asked about similar things

Quote

Use something like this:


	$criteria=new CDbCriteria;


		$criteria->select='AlId,AlDescr';  // only select the 'AlId' and 'AlDescr' columns


		


		$qAlbums=Album::model()->findAll($criteria);


		


		$albums = array();


		foreach($qAlbums as $p)


		{


			$albums[$p->AlId] = $p->AlDescr;


		}


		return $albums;


and then give $albums as parameter to CHtml::activeDropDownList($model, 'attribute', $albums);

ok, got u guys

:D

Hi Aztech, thanks for your advice, this is the sample that I looking at, I still new to Yii. By the way, the link you provided no more available… can you please update? I would like to have a look…

Thanks!