CakePhp: how to set a selection using $ this-> Form-> input with values ​​from 1 to 100?

I am using Cakephp and I would like to know how to set select with values ​​from 1 to 100?

Note that I prefer to use $this->Form->input , if possible.

+4
source share
1 answer

TL; DR:

 echo $this->Form->input('whatever', array( 'type'=>'select', 'options'=>array_combine(range(1,100), range(1,100)) )); 

Explanation:

PHP range creates the array of numbers (or letters) that you want for your options. But if you use range yourself, it creates:

 array(1,2,3,4... 

This will give you a dropdown number, but the values ​​will start from zero regardless of the number displayed - in this case you will get array(0=>1, 1=>2 ...

If you really want this:

 array(1=>1, 2=>2, 3=>3 ... 

Using array_combine , it simply makes the first option have the same value as the displayed number.

(obviously, you can write this in 1 line - I just broke it for readability)

+8
source

Source: https://habr.com/ru/post/1394222/


All Articles