Auto-generate selectbox options array with PHP

70 Views Asked by At

I've been looking all over, but I can't find a specific case on this. I'm writing a plugin for a open-source software program, and am using it's $form class and functions to render my forms without needing to manually insert in HTML.

I'm trying to make a Minimum Age (#) and Maximum Age (#) selectbox fields, each ranging from 1 to 100. I know something like this can be quickly done with something like

for ($i = 1; $i <= 99; $i++)

And then apply $i to a form in your webpage. But since this is a MVC program, I am bounded (for the most part) to what classes, objects and functions are available. When making a selectbox's actual options in this program, you typically do something like:

$field->setOptions(array(
   "Value"=>"Text Label",

rinse and repeat. However, since I'm trying to make a large list of numbers, I've been trying to find a way to somehow use range() or maybe array_push in place. I've tried the following thus far:

    function forAge(){
    for ($i = 1; $i <= 99; $i++){
        $array[] = $i;
    }}

    $tminage->setOptions(array(
        $array[]=>$array[]));

Or trying to pass the straightup "for" method and "range" method as the array's value. None of them work, either returning errors or displaying nothing on the selectbox. I'd like to know if there's any good way of working around this, or if it's a no-go because of the specific function wanting a simple array with a value and a label.

1

There are 1 best solutions below

2
On BEST ANSWER

Maybe i am missing something here but there is what I would do :

$minAgeOptions = array();
for ($i = 1; $i <= 100; $i++){
    $minAgeOptions[$i] = $i;
}

$tminage->setOptions($minAgeOptions);