Codeigniter 2 foreach()

I have <select> that displaying year. I want to check the registered years from the database.

Here's the image.

enter image description here

then here's my code in <select>

<select class="form-control select search-input-select col-lg-9 required_fields yearSelection registered_year" name="registered_year" style="border-color:red;">
   <?php
      // Sets the top option to be the current year. (IE. the option that is chosen by default).
      $currently_selected = date('Y');
      // Year to start available options at
      $earliest_year = 2018; 
      // Set your latest year you want in the range, in this case we use PHP to just set it to the current year.
      $latest_year = date('Y'); 
      echo '<option style="text-color:red;" selected disabled value>Select Year</option>';

      foreach ( range( $latest_year, $earliest_year ) as $i ) 
      {
         foreach($year_reg as $year)
         {
            $reg = date("Y", strtotime($year->date_created));

            if($reg == $i){
               echo '<option style="color:red;" value="'.$i.'"'.($i === $currently_selected ? ' selected="selected"' : '').' disabled>'.$i.'</option>';
            } else {
               echo '<option value="'.$i.'"'.($i === $currently_selected ? ' selected="selected"' : '').' >'.$i.'</option>';
            }
         }
      }
      ?>

</select>

So when the years from database and the earliest - latest years matched, it will become disabled and color red.

It displays 2019(w/ red text), 2019, 2018, 2018(w/ red text)

now my problem it displays multiple data. What seems to be the problem?

The problem is because of the inner loop.

Please try the below code. I just created an array to store years coming from the database, then checked whether $i is exists in $existing_years to mark the option to red.

  <?php

     $existing_years = []; 
     foreach ($year_reg as $year) {
         $existing_years[] = date("Y", strtotime($year->date_created));
     }
  ?>

Then changed the dropdown logic.

<select class="form-control select search-input-select col-lg-9 required_fields yearSelection registered_year"
        name="registered_year" style="border-color:red;">
    <?php
     echo '<option style="text-color:red;" selected disabled value>Select Year</option>';
    // Sets the top option to be the current year. (IE. the option that is chosen by default).
    $currently_selected = date('Y');
    // Year to start available options at
    $earliest_year = 2018;
    // Set your latest year you want in the range, in this case we use PHP to just set it to the current year.
    $latest_year = date('Y');

    foreach (range($latest_year, $earliest_year) as $i) {
        $selected = $i === $currently_selected ? ' selected="selected" ' : '';
        $style = (in_array($i, $existing_years)) ? ' style="color:red;" ' : '';
        $disabled = (in_array($i, $existing_years)) ? ' disabled ' : '';
        echo '<option value="' . $i . '"' . $selected . $style . $disabled . '>' . $i . '</option>';
    }
    ?>

</select>

Hope this will help.