4

I am new in yii. I have problem with fetching selected values in dropdown when updating the record.
I have dropdown with multiple selection of user's email.
When adding it is working fine, it allows me to select multiple values and can insert selected value's ids comma separated in database.
But the problem is when i want to update the record it displays only 1 selected record.

Here is my code:

in my View file:

<div class="controls">
    <?php echo $form->dropDownList($model, 'uid', $allUsers, array('class' => 'select2-me input-sel_large', 'multiple' => 'true', 'options' => array('' => array('selected' => true)))); ?>
        <?php echo $form->error($model, 'uid') ?>
</div>

in my Controller file:

$model = new User;
$allUsers = $model->getAllUsers();

in my Model file:

public function getAllUsers() {
        $arr = Yii::app()->db->createCommand()
        ->select('userEmail,pkUserID')
        ->from('tbl_user')
        ->queryAll();
        $users = array();
        if (is_array($arr)) {
            foreach ($arr as $value) {
                $users[$value['pkUserID']] = $value['userEmail'];
            }
        }
        return $users; 
    }

Can anyone help?

1 Answer 1

2

Example. Hope this help you.

class YourForm extends CFormModel
{
    public $uid = array(); // selected pkUserID's
}

//in action
$yourForm = new YourForm();
$yourForm->uid = array(1,2,3); // for example selected users with pk 1,2,3

$this->render('your_view', array('yourForm'=>$yourForm));

//view
/** @var CActiveForm $form */
/** @var YourForm $yourForm */
echo $form->dropDownList(
    $yourForm,
    'uid',
    CHtml::listData(User::model()->findAll(array('order' => 'userEmail ASC')), 'pkUserID', 'userEmail'),
    array('empty' => '', 'multiple'=>true))
   )
Sign up to request clarification or add additional context in comments.

8 Comments

when i am adding then that time it is working fine.. but i want to get selected values at the time of update it
@noddy Form - $form = $this->beginWidget('CActiveForm', array(...) in listData build listing of users. ask what you are interested please.
i have added selected values in db comma separated. and when updating the record i want that values as selected in dropdown
selected value gets from your $model->uid
what is instance of $model?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.