0

我有以下内容:

 `<select name="year" size="1" class="input" id="year" onChange="window.focus();" style="BACKGROUND-COLOR: #f7ed79; COLOR: #000000; font-family: Arial; font-size: 10px; width:60px;"> 
  <?php
  $sql = mysql_query("SELECT * FROM years");      
         while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
         echo("<option "); if($year==$row['year']) {echo "selected='selected'";}  
         echo ("value=\""); echo($row['year']); echo("\">"); echo($row['year']); 
         echo("</option>");
         };
         ?>
     </select>`

这给了我多年的选择余地。但我希望有可能查看 SELECT YEAR 之前的年份编号,无论年份先到。

这里有什么帮助吗?

谢谢

4

2 回答 2

0

我希望我能很好地理解它。

第一个选项:

<select name="year" size="1" class="input" id="year" onChange="window.focus();" style="BACKGROUND-COLOR: #f7ed79; COLOR: #000000; font-family: Arial; font-size: 10px; width:60px;">
<option selected="selected" disabled="disabled">Select year:</option>
<?php
      $sql = mysql_query("SELECT * FROM years");      
             while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
             echo("<option "); if($year==$row['year']) {echo "selected='selected'";}  
             echo ("value=\""); echo($row['year']); echo("\">"); echo($row['year']); 
             echo("</option>");
             };
 ?>
 </select>

第二个选项:

<select name="year" size="1" class="input" id="year" onChange="window.focus();" style="BACKGROUND-COLOR: #f7ed79; COLOR: #000000; font-family: Arial; font-size: 10px; width:60px;">
<optgroup label="Select year:">
<?php
      $sql = mysql_query("SELECT * FROM years");      
             while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
             echo("<option "); if($year==$row['year']) {echo "selected='selected'";}  
             echo ("value=\""); echo($row['year']); echo("\">"); echo($row['year']); 
             echo("</option>");
             };
 ?>
</optgroup></select>
于 2012-08-13T21:27:33.410 回答
0

这可能会使它更容易阅读。此外,您应该考虑使用mysqli_或 PDO 函数。

<select id="year" class="input" name="year" onchange="window.focus();" style="background-color: #f7ed79; color: #000000; font-family: Arial; font-size: 10px; width:60px;">
<?php
  $sql = mysql_query("SELECT * FROM years") or die(mysql_error());

  while ($row = mysql_fetch_assoc($sql))
  {
    $selected_value = '';

    //not sure where $year is populated
    if($row['year'] == $year)
    {
      $selected_value = ' selected="selected"';
    }
?>
     <option value="<php echo $row['year'] ?>"<?php echo $selected_value ?>><php echo $row['year'] ?></option>
<?php
     };
?>
</select>
于 2012-08-13T21:27:50.857 回答