我有这个PHP代码,它在过去10年和未来10年的SELECT框中填充值。
<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>
如何让它自动选择当前年份?
答案 0 :(得分:7)
<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>';
答案 1 :(得分: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>';
}
}
答案 2 :(得分:1)
将您的回音更改为:
echo '<option'.($starting_year == date('Y')) ? "selected=\"selected\"" : "".' value="'.$starting_year.'">'.$starting_year.'</option>';
答案 3 :(得分:1)
<?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>
答案 4 :(得分: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>';
}
}
请使用上面的代码循环,而不是你的。
答案 5 :(得分: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>';
答案 6 :(得分:0)
您应该使用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 :(得分: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>';
}