Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.0 Select form Multiple selected

I have have this dropdown menu where you can select multiple values. Now let's say I want to edit my info and make a dropdown menu with multiple selected values. Trying to figure out how it goes, but no results.

Let's say I have:

$selected = array(3, 4);
$options = array(1,2,3,4);

echo $this->Form->select('Attendees', $options,array('multiple' => true, 'selected' => $selected));

I've used this code, but nothing is selected.

like image 856
hope_industries Avatar asked Sep 19 '12 09:09

hope_industries


1 Answers

Ok found a way, appearantly it needs to be like this:

$selected = array(2, 3);
$options = array(1, 2, 3, 4);

echo $this->Form->input('Attendees', array('multiple' => true, 'options' => $options, 'selected' => $selected));

Will output:

  • 1
  • 2
  • 3 checked
  • 4 checked

The $selected checks the index key of each element rather the value itself.

like image 131
hope_industries Avatar answered Sep 30 '22 01:09

hope_industries