-1

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.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

3 Answers3

1

you can use php range function

$options = array_combine(range(1, $stock), range(1, $stock));

$this->Form->select('quantity', $options, ['empty' => 'Quantity'])
arilia
  • 9,373
  • 2
  • 20
  • 44
0
$arr=[];
for($i = 0;$i<$stock;i++) array_push($arr, $i); 

$this->Form->select('quantity', $arr, ['empty' => 'Quantity'])
Valentin Rapp
  • 432
  • 6
  • 11
0

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'])
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • Thanks @pradeep, but there is a problem here... value assigned are from 0 onwards like `` I want to assign value from 1 `` – Anuj TBE Jul 20 '16 at 06:46
  • 1
    use this > $options = range(0,$stack); unset($options[0]); then use $options – Pradeep Jul 20 '16 at 07:02