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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。