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

24k Views Asked by At

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..............

6

There are 6 best solutions below

0
On BEST ANSWER

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);
0
On

Old version of codeigniter doesn't have form_multiselect(). Next code should work

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

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>
1
On
$options = array(
  'small'  => 'Small Shirt',
  'med'    => 'Medium Shirt',
  'large'   => 'Large Shirt',
  'xlarge' => 'Extra Large Shirt',
);    
echo form_dropdown('shirts[]',$options);
0
On

We have a one array and we want to show selected values of this array in multi selector

$show_selected = array("USA", "Poland", "Japan");

For this I have used in_array to show selected values in selector

<select class="form-control js-example-basic-multiple" multiple="multiple">
  <option value="" disabled selected>Choose your country</option>
  <option <?php if(in_array(1,$show_selected)) echo "selected";?>  value="1">USA</option>
  <option <?php if(in_array(2,$show_selected)) echo "selected";?> value="2">Germany</option>
  <option <?php if(in_array(3,$show_selected)) echo "selected";?> value="3">France</option>
  <option <?php if(in_array(4,$show_selected)) echo "selected";?>  value="4">Poland</option>
  <option <?php if(in_array(5,$show_selected)) echo "selected";?> value="5">Japan</option>
</select>
<button class="btn-save btn btn-primary btn-sm">Save</button>
0
On

i tried every solution but no one works with me i tried ( form_dropdown from from helper) i also tried ordinary way with multiple= "multiple"

is it common problem with codeigniter ??

Update the error was that anyone forget to name in html attribute as array cars[]

<select **name="cars[]"** multiple="multiple">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

this works fine always .