У меня есть этот код 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>
как я могу автоматически выбрать текущий год?
<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>';
Проверьте год с текущим годом и выберите его.
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>'; } }
Измените свое эхо:
echo '<option'.($starting_year == date('Y')) ? "selected=\"selected\"" : "".' value="'.$starting_year.'">'.$starting_year.'</option>';
<?php //get the current year $Startyear=date('Y'); $endYear=$Startyear-10; // set start and end year range ie 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>
в<?php //get the current year $Startyear=date('Y'); $endYear=$Startyear-10; // set start and end year range ie 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>
в<?php //get the current year $Startyear=date('Y'); $endYear=$Startyear-10; // set start and end year range ie 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>
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>';
Вы должны использовать выбранные атрибуты
$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>'; }
for($starting_year; $starting_year <= $ending_year; $starting_year++) { echo '<option value="'.$starting_year.'"'. if(starting_year == date('Y')) echo "selected" .'>'.$starting_year.'</option>'; }
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>'; } }
Пожалуйста, используйте код выше для цикла, а не ваш.