How Can I Get A Full Userlist From Database In An Array Like('userid'=>'username', 'userid2'=>'username2')...

Dear all,

Is there a way I can get all the users from the database, So they can be display in the following case?




‘3’=>‘my user id is3’,


And so on…

Any ideas?


sorry, I’m not really understand what you really want to do. maybe you can give the surrounding codes? and if you want to display it only, why not you start with find the users first, save it into variabel and then echo it?

Thanks for your help…

I have a multiple dropdown, which I can choose users from it, its going to show the username but the value is going to be the user’s id.

For example, user ‘demo’ s user_id is 102, When I choose “demo” from the dropdown, the value I choose should be 102.

Here is what I have now, but it’s not giving me “user_id”=>‘username’


 'condition'=>'sender=:userId AND accept = 1',

'params' => array(




foreach($friends1 as $friend1){

$newarray = array($friend1->receiver => $friend1->receiverx->username); 

array_merge($list, $newarray);


However, after doing this , the array $list is still empty…

this is how I create a dropdownlist with database :

  1. in the form:

<?php //echo $form->dropDownList($model,'id_user', User::model()->getOptions()); ?>

  1. in the User model:

public function getOptions() {

        $users = self::model()->findAll(array('order' => 'user_name'));

        $array = CHtml::listData($users, 'id_user', 'user_name');

        $empty = array('' => 'Choose User');

        return $empty + $array;


this dropdownlist will show the value of username and give you the id_user value of the chosen username.

Thanks for your help…

I am using another table different than users.

since listData doesn’t support relations. I have to use some stupid ways to achieve this.

Anyway I just fixed the problem. Thanks so much for your help!

yeah, I just give you some example. it’s your job to modify it.

your welcome anyway :)