Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 742
Dacobah Avatar asked Jan 17 '23 14:01

Dacobah


1 Answers

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)

like image 163
Dave Avatar answered Jan 22 '23 05:01

Dave