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>

2番目のオプション:

<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 に答える