I have the following code that is supposed to generate a select form with school years as options inside, such as 2010-2011, 2011-2012, etc.
However, for some reason the select form it is generating is only incrementing the first year while the second year gets stuck at the last array value, such as 2010-2020, 2011-2020, 2012-2020, etc.
Can someone please help me understand why? Thank you!
Here is my code:
/*
* This function returns the HTML code necessary to be echoed out to produce
* a drop-down box input form for a range of 15 years with the current year selected.
*
* $name $name is the variable string for the drop-down select form's identifying name
* $prepopulated_info $prepopulated_info is the year that is to be prepopulated. The
* range of years is populated based on what this number is.
*/
function schoolYearSelectFormGenerator($name, $prepopulated_info) {
// The current year is the already selected default pre-populated year
if($prepopulated_info == 'Y')
$selected = date($prepopulated_info);
else
$selected = $prepopulated_info;
// r is for range of choices
$r = range(date('Y') - RANGE_OF_YEARS, date('Y') + RANGE_OF_YEARS);
//create the HTML select
$string = '<select name="' . $name . '" id="year" class="form-control">';
$i = 0;
$yearPlusOne = array();
foreach($r as $year)
{
$yearPlusOne[$i] = $year + 1;
$string .= "<option value=\"$year - $yearPlusOne[$i]\" class='normal_select'";
$string .= ($year - $yearPlusOne[$i] = $selected) ? ' selected="selected"' : '';
$string .= ">$year - $yearPlusOne[$i]</option>\n";
$i++;
}
$string .= '</select>';
return $string;
}