• support@answerspoint.com

How to set selection in mulitiselect list box in cakephp

1653

 I want to set the multi selection to my multi list box.

I have two results in my view page.

$inr1 = 0;
$arr1 = array();
$str_arr = '';
foreach ($result as $rows){
    $inr1 = $rows['Employees']['employee_id'];
    $arr1[$inr1] = $rows['Employees']['first_name'].' '.$rows['Employees']['last_name'];
    $str_arr = $str_arr.$inr1.',';
}
$str_arr = substr($str_arr,0,-1);
//print_r($arr1);

$inr = 0;
$arr = array();
foreach ($options as $option){
    $inr = $option['Employee']['employee_id'];
    $arr[$inr] = $option['Employee']['first_name'].' '.$option['Employee']['last_name'];
}

//print_r($arr);

echo $this->Form->input('emp_id_one', array(    'options' => array( $arr),
        'empty' => '(choose one)',
        'div'=>'formfield',
        'error' => array(   'wrap' => 'div',
                            'class' => 'formerror'
                        ),
        'label' => 'Team Members',
        'type' => 'select', 'multiple' => true,
        'selected' => $arr1,
        'style' => 'width:210px; height:125px;',

));

But the values in $arr1 not selected in the list box.

The $arr1 have the selected values.

The $arr have the options to the list.

The problem is that the selction is not working..There have no selection...

How can i do this?

If there is any problem in my code..?

1Answer


0

Finally i solved my problem by chaing some code:

Add the one line of code after this $str_arr = substr($str_arr,0,-1);'. That is....

$str_arr = substr($str_arr,0,-1);
    $sel = explode(',',$str_arr);

Then change the name of variable as like this :

echo $this->Form->input('emp_id_one', array(    'options' => array( $arr),
        'empty' => '(choose one)',
        'div'=>'formfield',
        'error' => array(   'wrap' => 'div',
                            'class' => 'formerror'
                        ),
        'label' => 'Team Members',
        'type' => 'select', 'multiple' => true,
        'selected' => $sel,
        'style' => 'width:210px; height:125px;',

    ));

Now the values in $sel is selected in the multi list

  • answered 8 years ago
  • B Butts

Your Answer

    Facebook Share        
       
  • asked 9 years ago
  • viewed 1653 times
  • active 9 years ago

Best Rated Questions