开发者

Dropdown of Numbers

is there a built-in helper that creates a drop-down/select of numbers from start to end like 1 to 50

<select>
  <option value='1'>1</option>
  <option value='2'>2</option>
  <option value='3'>3</option>
  <option value='4'>4</option>
开发者_Python百科  <option value='5'>5</option>
</select>

I dont want to create a custom helper or use for loop


There is no "magical cakephp way" to do this. The best way is to implement this is in the controller function you put:

$numbers = array();
for($i = 1; $i < 50; $i++) {
    array_push($numbers, $i);
}
$this->set('numbers', $numbers);

Then in the view:

$this->Form->input('numbers', array('type' => 'select', 'options' => $numbers));

I am sure there are other methods, but this is by far the simplest.

UPDATE: If you prefer you can use:

foreach(range(1, 50) as $number) {
   array_push($numbers, $number);
}


Just do:

$this->Form->input('numbers', array('type' => 'select', 'options' => range(0, 50)));

if you need fomething more complicated like like number starting from 5 or so do:

$options = range(5, 20);
$this->Form->input('numbers', array('type' => 'select', 'options' => array_combine($options, $options)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜