Currently my code shows the year from 1885 to 2017. What this does is show the years like so 0 = 1885, 1 = 1886.
What I want is that it shows it like this 1885 = 1885, 1886 = 1886 etc. (This resembles the date a car is built)
$options = [];
for ($x = 1885; $x <= date("Y"); $x++) {
array_push($options, $x);
}
echo $this->Form->select('year', [
$options,
],[
'label' => false,
'class' => 'form-group form-control',
]);
So I was wondering how to fix what I did wrong.
for ($x = 1885; $x <= date("Y"); $x++) {
$options[$x] = $x;
}
You need to set the keys of the $options
array as the keys are used as values to be submitted. You can as well benefit from using the range
function to generate the array:
$values = array_map('strval', range(1885, (int) date("Y")));
$options = array_combine($values, $values);
You can also do this using range()
and array_combine()
:-
$years = range(1985, date('Y'));
$options = array_combine($years, $years);