I am using Cakephp and I would like to learn how to set a select with values from 1 to 100?
Please notice I prefer to use $this->Form->input
if possible.
TLDR:
echo $this->Form->input('whatever', array(
'type'=>'select',
'options'=>array_combine(range(1,100), range(1,100))
));
Explanation:
PHP's range
creates an array of numbers (or letters), which is what you want for your options. But if you use range
by itself, it creates:
array(1,2,3,4...
This would give you a dropdown of numbers, but the values will start with zero regardless of the displayed number - in this case, you'd end up with array(0=>1, 1=>2 ...
When you really want this:
array(1=>1, 2=>2, 3=>3 ...
By using array_combine
just makes it so the first option has the same value as the displayed number.
(obviously you can write this in 1 line - I just broke it up for ease of reading)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With