2014-05-09 62 views
0
<select id="section" name="section"> 
    <?php 
    include("Nethost.php"); 
    $section = ""; 
    $yr = ""; 
    $sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section"); 
    while ($row = mysql_fetch_array($sql)){ 
     $section = $row['section']; 
     $yr = $row['yrlvl']; 
     ?> 
     <option value="">Select</option> 
     <option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'"); 
     if(mysql_num_rows($result2) > 0) { ?> 
      selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option> 
    <?php } ?> 
</select> 

以上是select選項的php代碼。從數據庫表填充數據,如何將第一個值設置爲空。我嘗試添加一個選擇,但這是結果enter image description here將動態HTML選項的第一個值設置爲空

選項選擇不斷重複。該怎麼做?

回答

2

試試這個

<select id="section" name="section"> 
    <?php 
    include("Nethost.php"); 
    $section = ""; 
    $yr = ""; 
    $sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section"); 
    ?> 
    <option value="">Select</option> 
    <?php 
    while ($row = mysql_fetch_array($sql)){ 
     $section = $row['section']; 
     $yr = $row['yrlvl']; 
     ?> 

     <option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'"); 
     if(mysql_num_rows($result2) > 0) { ?> 
      selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option> 
    <?php } ?> 
</select> 
+0

仙。只是把它放在while循環之外。謝謝。 – eirishainjel

+1

:)雅,如果這解決了您的問題,請記住標記答案正確 – Baig

+0

是的。我仍然有一分鐘將它標記爲答案。謝謝。 – eirishainjel

1

只需推動該選項行 「選擇」 出你的PHP代碼:

<select id="section" name="section"> 
    <option value="">Select</option> 
    <?php 
    ...your php code 
    ?> 
</select> 
相關問題