Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a multi select box with out selected options codeigniter

hi i am using codeigniter , i want to add a multi select box to my page ,

i saw the codeigniter user guide example , but what it is doing is set the values in multi select .

like this

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, $shirts_on_sale);

in this multi select box created like this

<select name="shirts" multiple="multiple">
<option value="small" selected="selected">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

it have to give the options to be selected in $shirts_on_sale array , but in my case i want to create a multi select but dont want selected options i tried to pass an empty array . but it is not working

like this

$array = array();
echo form_dropdown('shirts', $substore_details, $array); 

how to create a multi select with no selected items . please help..............

like image 573
Kanishka Panamaldeniya Avatar asked Oct 19 '11 11:10

Kanishka Panamaldeniya


2 Answers

You should use the form_multiselect() helper.

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

echo form_multiselect('shirts', $options);
like image 81
Cubed Eye Avatar answered Nov 08 '22 02:11

Cubed Eye


The only thing that comes to my mind is using an array with more than one empty element:

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$array = array('','');
echo form_dropdown('shirts',$options, $array);

This code works, though not the most elegant out there.

UPDATE:

This is even better, didn't remember it at first!

echo form_multiselect('shirts',$options,'','');

Output:

<select name="shirts" multiple="multiple">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>
like image 30
Damien Pirsy Avatar answered Nov 08 '22 00:11

Damien Pirsy