Reputation: 99
I'm instructed to create a select box that allows the user to select a year ranging from the current year - 5 to 2050. The default year needs to be the current year. Right now the list is starting at 2014 (current year -5).
I'm having trouble making the default year display as 2019 rather than 2014. We are suppose to use the DateTime object and formats that go with the DateTime class.
I'm including code I also wrote for the select box that allows the user to choose a month. I would appreciate feedback on that as well, if it's appropriate to ask. I also have to create a checkbox that allows the user to select the entire year. I've created the checkbox but do not know how to even begin to code it to select the entire year.
edited to add: This assignment must be done using only PHP and HTML with some CSS styling.
<section>
<form id="yearForm" name="yearForm" method="post" action="">
<label for="select_year">Select the year: </label>
<?php
// Sets the default year to be the current year.
$current_year = date('Y');
// Year to start available options.
$earliest_year = ($current_year - 5);
// Set your latest year you want in the range.
$latest_year = 2050;
echo '<select>';
// Loops over each int[year] from current year, back to the $earliest_year [1950]
foreach ( range( $earliest_year, $latest_year ) as $i ) {
// Echos the option with the next year in range.
echo '<option value="'.$i.'" '.($i === $current_year ? ' selected="selected"' : '').'>'.$i.'</option>';
}
echo '</select>';
?>
</form>
<br />
<br />
<form id="monthForm" name="monthForm" method="post" action="">
<label for="month">Select the month: </label>
<!-- <input type=hidden id="month" name=month>-->
<select id="month" name="month" >
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<br />
<br />
<label for="whole_year">Show whole year: </label>
<input type="checkbox" id="whole_year" name="whole_year" >
<br />
<br />
<input type="submit" class=inline name="submitButton" id="submitButton" value="Submit" />
</form>
Upvotes: 0
Views: 419
Reputation: 5224
$i
is an integer and $current_year
is a string so with strict comparison, ===
, these don't match. Use ==
for your comparison and it should work.
($i == $current_year ? ' selected="selected"' : '')
For more about this see http://php.net/manual/en/language.operators.comparison.php
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
Upvotes: 3