doudouxuqh198138 2019-02-08 07:09
浏览 17
已采纳

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?

  • 写回答

1条回答 默认 最新

  • drdl18946 2019-02-08 08:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)