PHP Dropdown populating years - how to select curr

2020-07-14 06:03发布

I have this PHP Code that is populating values in a SELECT box for the previous 10 years and the next 10 years.

<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>

how can I make it automatically select the current year?

标签: php
8条回答
\"骚年 ilove
2楼-- · 2020-07-14 06:34

Change your echo to:

 echo '<option'.($starting_year == date('Y')) ? "selected=\"selected\"" : "".' value="'.$starting_year.'">'.$starting_year.'</option>';
查看更多
闹够了就滚
3楼-- · 2020-07-14 06:35
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>';
    }
}

Please use the code above for loop, instead of yours.

查看更多
Anthone
4楼-- · 2020-07-14 06:39

Check the year with current year and make it selected.

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>';
    }
} 
查看更多
该账号已被封号
5楼-- · 2020-07-14 06:41
<?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>
查看更多
Luminary・发光体
6楼-- · 2020-07-14 06:48
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>';
查看更多
三岁会撩人
7楼-- · 2020-07-14 06:48

You should use the 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>';
}
查看更多
登录 后发表回答