CakePHP 3:在select |中动态创建选项 表格助手
I want to generate select
input field like
<select name="quantity">
<option value="">Quantity</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
where option is generated dynamically.
There is a stock
column which contains int value. I want to generate options as much as value in stock. Ex: If stock
value is 5 then option will be from 1
to 5
.
I can generate select input field by
$this->Form->select('quantity', [1,2,3,4,5], ['empty' => 'Quantity'])
But here options generated will of 5 length. I want it to be generated as per the value in stock
column.
我想生成 其中选项是动态生成的。 p>
有一个 我可以 生成选择输入字段 p>
但这里生成的选项长度为5。 我希望它是根据 select code>输入字段,如 p>
&lt; select name =“quantity”&gt;
&lt; option value =“”&gt; Quantity&lt; / option&gt;
&lt; option value =“1”&gt; 1&lt; / option&gt;
&lt; option value =“2”&gt; 2&lt; / option&gt;
&lt; option value =“3”&gt; 3&lt; / option&gt;
&lt; / select&gt;
code> pre>
stock code>列,其中包含int值。 我想生成与股票价值一样多的期权。 例如:如果
stock code>值为5,则选项将从
1 code>到
5 code>。 p>
$ this-&gt; Form-&gt; select('quantity',[1,2,3,4,5],['empty' =&gt;'数量'])
code> pre>
stock code>列中的值生成的。 p>
div>
First set your $stack variable value in controller using set (Simple way) :
$this->set('stack', $stack);
use that variable like this in your view:
$options = range(1,$stack);
$this->Form->select('dropdown name', $options, ['empty' => 'Quantity'])
$arr=[];
for($i = 0;$i<$stock;i++) array_push($arr, $i);
$this->Form->select('quantity', $arr, ['empty' => 'Quantity'])
you can use php range function
$options = array_combine(range(1, $stock), range(1, $stock));
$this->Form->select('quantity', $options, ['empty' => 'Quantity'])