1

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;
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
Bryan Miller
  • 3,262
  • 4
  • 27
  • 51
  • Probably related to `$year - $yearPlusOne[$i] = $selected` – Sean Jul 16 '15 at 19:30
  • 1
    why are you making `$yearPlusOne` an array? seems kind of pointless to create a parallel array where the values are simply `+1` from the main $r array. – Marc B Jul 16 '15 at 19:30
  • `$year - $yearplusone = $selected` is flat-out wrong... – Marc B Jul 16 '15 at 19:32
  • I originally had just a +1 from the main $r array, but I was trying to figure out what was causing the value to get stuck. Indeed, it was = vs == error – Bryan Miller Jul 16 '15 at 19:39

2 Answers2

3

Fix this part:

$string .= ($year - $yearPlusOne[$i] = $selected) ? ' selected="selected"' : '';

to

$string .= ($year - $yearPlusOne[$i] == $selected) ? ' selected="selected"' : ''; 
//                                   ^== here.

because $year - $yearPlusOne[$i] = $selected evaluated and overwrites your $yearPlusOne[$i] value.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

Modify the loop near the bottom to:

foreach($r as $year)
{
    $yearPlusOne[$i] = $year + 10;
    $string .= "<option value=\"$year - $yearPlusOne[$i]\" class='normal_select'";
    $string .= ($year - $yearPlusOne[$i] = $selected) ? ' selected="selected"' : '';
    $string .= ">$year - $yearPlusOne[$i]</option>\n";
    $i++;
}
Crackertastic
  • 4,958
  • 2
  • 30
  • 37