Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

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

Upvotes: 7

Views: 24257

Answers (6)

user12856850
user12856850

Reputation: 1

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>

Upvotes: 0

Parveen Chauhan
Parveen Chauhan

Reputation: 1496

$options = array(
  'small'  => 'Small Shirt',
  'med'    => 'Medium Shirt',
  'large'   => 'Large Shirt',
  'xlarge' => 'Extra Large Shirt',
);    
echo form_dropdown('shirts[]',$options);

Upvotes: 0

Dev_meno
Dev_meno

Reputation: 124

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 .

Upvotes: 1

user1189422
user1189422

Reputation: 1

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

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

Upvotes: 0

Cubed Eye
Cubed Eye

Reputation: 5631

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

Upvotes: 11

Damien Pirsy
Damien Pirsy

Reputation: 25435

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>

Upvotes: 1

Related Questions