【问题标题】:PHP Dropdown populating years - how to select current yearPHP Dropdown 填充年份 - 如何选择当前年份
【发布时间】:2016-02-02 20:09:39
【问题描述】:

我有这个 PHP 代码在 SELECT 框中填充过去 10 年和未来 10 年的值。

<select name="fromYear"';
   $starting_year  =date('Y', strtotime('-10 year'));
   $ending_year = date('Y', strtotime('+10 year'));

    for($starting_year; $starting_year <= $ending_year; $starting_year++) {
 echo '<option value="'.$starting_year.'">'.$starting_year.'</option>';
  }             
 echo '<select>

如何让它自动选择当前年份?

【问题讨论】:

  • 您可以使用$curyear = date('Y') 获取当前年份,因此只需检查循环内的内容即可。

标签: php


【解决方案1】:
<select name="fromYear"';
 $starting_year  =date('Y', strtotime('-10 year'));
 $ending_year = date('Y', strtotime('+10 year'));
 $current_year = date('Y');
 for($starting_year; $starting_year <= $ending_year; $starting_year++) {
     echo '<option value="'.$starting_year.'"';
     if( $starting_year ==  $current_year ) {
            echo ' selected="selected"';
     }
     echo ' >'.$starting_year.'</option>';
 }               
 echo '<select>';

【讨论】:

    【解决方案2】:
    <?php
    //get the current year
    $Startyear=date('Y');
    $endYear=$Startyear-10;
    
    // set start and end year range i.e the start year
    $yearArray = range($Startyear,$endYear);
    ?>
    <!-- here you displaying the dropdown list -->
    <select name="year">
        <option value="">Select Year</option>
        <?php
        foreach ($yearArray as $year) {
            // this allows you to select a particular year
            $selected = ($year == $Startyear) ? 'selected' : '';
            echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
        }
        ?>
    </select>
    

    【讨论】:

    • 最好按照规范定义selected$selected = ($year == $Startyear) ? 'selected="selected"' : '';,否则浏览器可能会忽略选中状态。
    【解决方案3】:

    用当前年份检查年份并选择它。

    for($starting_year; $starting_year <= $ending_year; $starting_year++) {
        if($starting_year == date('Y')) {
            echo '<option value="'.$starting_year.'" selected="selected">'.$starting_year.'</option>';
        } else {
            echo '<option value="'.$starting_year.'">'.$starting_year.'</option>';
        }
    } 
    

    【讨论】:

      【解决方案4】:

      将您的回声更改为:

       echo '<option'.($starting_year == date('Y')) ? "selected=\"selected\"" : "".' value="'.$starting_year.'">'.$starting_year.'</option>';
      

      【讨论】:

        【解决方案5】:
        echo '<select name="fromYear">';
        $cur_year = date('Y');
        for($year = ($cur_year-10); $year <= ($cur_year+10); $year++) {
            if ($year == $cur_year) {
                echo '<option value="'.$year.'" selected="selected">'.$year.'</option>';
            } else {
                echo '<option value="'.$year.'">'.$year.'</option>';
            }
        }               
        echo '<select>';
        

        【讨论】:

          【解决方案6】:

          您应该使用selected attributed

          $starting_year  =date('Y', strtotime('-10 year'));
          $ending_year = date('Y', strtotime('+10 year'));
          for($starting_year; $starting_year <= $ending_year; $starting_year++) {
             if(date('Y')==$starting_year) { //is the loop currently processing this year?
                $selected='selected'; //if so, save the word "selected" into a variable
             } else {  
                $selected='' ; //otherwise, ensure the variable is empty
             }
             //then include the variable inside the option element
             echo '<option '.$selected.' value="'.$starting_year.'">'.$starting_year.'</option>';
          }
          

          【讨论】:

            【解决方案7】:
            for($starting_year; $starting_year <= $ending_year; $starting_year++) { 
             echo '<option value="'.$starting_year.'"'. if(starting_year == date('Y')) echo "selected"  .'>'.$starting_year.'</option>';   
            
            } 
            

            【讨论】:

              【解决方案8】:
              for($starting_year; $starting_year <= $ending_year; $starting_year++) {
                  if($starting_year == date('Y')){
                      echo '<option selected=selected value="'.$starting_year.'">'.$starting_year.'</option>';
                  }else{
                      echo '<option value="'.$starting_year.'">'.$starting_year.'</option>';
                  }
              }
              

              请使用上面的代码 for 循环,而不是你的。

              【讨论】:

                猜你喜欢
                • 2019-02-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-01-06
                • 2015-09-28
                • 1970-01-01
                相关资源
                最近更新 更多