PHP with HTML - Put an array inside a select html

1

Hello, I am a beginner and I am with the following question, I have an array, where one of the parameters comes from the bank, where the number 5 is from the bank.

$inicio = 1;
$ultimo = 5; //esse valor vem do banco.
$arr = range($inicio, $ultimo);
print_r($arr);

With the above code I get an array of 1 from the number registered in the bank, in this example the 5.

Following the example I would like to put this array in a select html, from 1 to 5 in case, I tried with but I did not succeed, can anyone give me a light? Thank you

    
asked by anonymous 26.12.2014 / 16:55

2 answers

0

You can use the command foreach or for as follows:

<select>
<?php
    foreach ( $arr as $k => $v ) {
        echo "<option value=\"" . $k . "\">" . $v . "</option>";
    }
?>
</select>

The variable $k contains the key that indexes the array, which can be either a numeric key or an associative key (a string); $v contains the value of the element in $arr[$k] position.

That is:

$arr[$k] === $v // true
    
26.12.2014 / 17:10
0

You can use this:

<?
$inicio = 1; 
$ultimo = 5; //esse valor vem do banco.

$arr = range($inicio, $ultimo);

print_r($arr);
?>
<select name="n">
<?
foreach($arr as $n){ 
?>

<option name = "n"value="<? echo $n ?>"><? echo $n ?></option>

<? 
} 
?>
</select>

This will display a SELECT in HTML, with the options become $ n which is the same as $ arr.

For each $ n it will display an OPTION within a SELECT.

    
26.12.2014 / 17:10