hillz hillz - 1 month ago 5
PHP Question

PHP foreach doesn't show the first array value

echo 'SSH Timeout: <select name="ssh_timeout"';
$time = array('1','5','15','30','60');
foreach ($time as $value) {
if (15 == $value) {
echo "<option value='$value' selected>$value seconds</option>";
}
else{
echo "<option value='$value'>$value seconds</option>";
}
}
echo '</select>';


That code doesn't show the first value which is
1
but instead it starts from
5
to
60
. How do I fix it ?

Answer

You should close the opening <select tag, otherwise the <option value='1' gets inside the <select and the browser will not render it.

This is the fix:

echo 'SSH Timeout: <select name="ssh_timeout">';
$time = array('1','5','15','30','60');
    foreach ($time as $value) {
        if (15 == $value) {
            echo "<option value='$value' selected>$value seconds</option>";
        }
        else{
            echo "<option value='$value'>$value seconds</option>";
        }
    }
    echo '</select>';