5

我有这个 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>

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

4

8 回答 8

8
<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>';
于 2013-06-26T12:43:19.527 回答
4
<?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>
于 2016-02-02T12:09:39.437 回答
2

用当前年份检查年份并将其选中。

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>';
    }
} 
于 2013-06-26T12:41:16.400 回答
1

将您的回声更改为:

 echo '<option'.($starting_year == date('Y')) ? "selected=\"selected\"" : "".' value="'.$starting_year.'">'.$starting_year.'</option>';
于 2013-06-26T12:41:41.807 回答
0
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 循环,而不是你的。

于 2013-06-26T12:44:31.583 回答
0
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>';
于 2013-06-26T12:44:47.290 回答
0

您应该使用选定的属性

$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>';
}
于 2013-06-26T12:45:31.793 回答
0
for($starting_year; $starting_year <= $ending_year; $starting_year++) { 
 echo '<option value="'.$starting_year.'"'. if(starting_year == date('Y')) echo "selected"  .'>'.$starting_year.'</option>';   

} 
于 2013-06-26T12:48:15.973 回答