2013-06-26 6 views
5

Ho questo codice PHP che sta compilando i valori in una casella SELECT per i 10 anni precedenti e per i prossimi 10 anni.PHP Dropdown popolamento anni - come selezionare l'anno corrente

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

come posso selezionare automaticamente l'anno corrente?

+5

È possibile ottenere l'anno in corso con '$ curyear = data ('Y')' quindi basta verificare che all'interno del vostro ciclo . – Nelson

risposta

2

Controllare l'anno con l'anno in corso e renderlo selezionato.

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>'; 
    } 
} 
1

Modifica la tua eco:

echo '<option'.($starting_year == date('Y')) ? "selected=\"selected\"" : "".' value="'.$starting_year.'">'.$starting_year.'</option>'; 
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>'; 
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>'; 
    } 
} 

Si prega di utilizzare il codice qui sopra per ciclo, invece del tuo.

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>'; 
0

È consigliabile utilizzare il 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>'; 
} 
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>'; 

} 
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> 
+0

sarebbe meglio impostare il 'selected' come definito dalla specifica:' $ selected = ($ year == $ Startyear)? 'selected = "selected"': ''; 'altrimenti i browser potrebbero ignorare lo stato selezionato. – SaschaM78

Problemi correlati