php 如何创建一个没有选定选项的多选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7820946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:25:59  来源:igfitidea点击:

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

phpcodeignitervalidationcodeigniter-form-helper

提问by Kanishka Panamaldeniya

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

嗨,我正在使用codeigniter,我想multi select box在我的页面中添加一个,

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

我看到了 codeigniter 用户指南示例,但它所做的是在多选中设置值。

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_salearray , but in my case i want to create a multi select but dont want selected optionsi tried to pass an empty array . but it is not working

它必须提供要在$shirts_on_sale数组中选择的选项 ,但在我的情况下,我想创建一个多选,但dont want selected options我试图传递一个空数组。但它不起作用

like this

像这样

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

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

如何创建没有选定项目的多选。请帮忙..............

回答by Cubed Eye

You should use the form_multiselect() helper.

您应该使用 form_multiselect() 助手。

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

echo form_multiselect('shirts', $options);

回答by Damien Pirsy

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>

回答by Dev_meno

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"

我尝试了所有解决方案,但没有人与我合作 我尝试过(来自助手的 form_dropdown) 我也尝试了使用 multiple=“multiple” 的普通方法

is it common problem with codeigniter ??

这是 codeigniter 的常见问题吗??

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

更新错误是有人忘记在 html 属性中命名为数组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 .

这总是很好用。

回答by Parveen Chauhan

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

回答by user12856850

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

为此,我使用 in_array 在选择器中显示选定的值

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

回答by user1189422

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

旧版本的 codeigniter 没有form_multiselect(). 下一个代码应该可以工作

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