2013-03-08 85 views
0

我有這樣的PHP代碼有一個下拉從數據庫中提取信息,但似乎無法弄清楚如何讓下拉選擇對列表進行排序...製作下拉排序列表

下拉正顯示出課程名稱作爲標籤,但選中時需要按課程ID對列表進行排序。

Visit Here to see the table in action

<?php 
     $connect = mysql_connect('localhost','emscompl_paramed','PASSOWRD) or die(mysql_error()); 

     $selectdb = mysql_select_db('emscompl_joom1283',$connect); 

     $sel = "SELECT us.fullname, s . * 
FROM registered_users AS `us` 
LEFT OUTER JOIN course_students AS s ON s.userid = us.userid"; 
     $ressel = mysql_query($sel); 

     $fetchsel = mysql_fetch_array($ressel); 

     ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
.titiel { 
    font-weight: bold; 
} 

td { 
    border-right-width: thin; 
    border-bottom-width: thin; 
    border-right-style: dotted; 
    border-bottom-style: solid; 
    text-align:center; 
} 
th { 
     background-color:#000000; 
     color: #FFF; 
    } 
    tr:nth-child(odd) { background-color:#eee; } 
tr:nth-child(even) { background-color:#fff; } 
</style> 
</head> 

<body> 
<p class="titiel">Pre-Entrance Document Report</p> 
<p> Please Select Course for Report</p> 
    <form method="post" action="preentrancereportsorted.php"> 
     <label for="select"><select name="course" value="Select" size="1"> 
    <?php 
    $sql = "SELECT * FROM courses"; 
    $result = mysql_query($sql) or die (mysql_error()); 
     while ($row = mysql_fetch_array($result)) 
     { 
      $id=$row["id"]; 
       $course=$row["coursename"]; 
       $options.="<OPTION VALUE=\"$id\">".$course; 
     } 
     ?> 
      <option> 
       <? echo $options ?> 
       </option> 
      </select> 
      <input type="submit" name="Submit" value="Generate Report"> 
    </form> 
<table width="1246" height="56"> 
    <tr> 
    <th width="147" height="50">Student Name</td> 
    <th width="15">R</th> 
    <th width="18">M</th> 
    <th width="18">L</th> 
    <th width="81">Background</th> 
    <th width="83">Drug Screen</th> 
    <th width="112">Clear Background</th> 
    <th width="113">Clean Drug Screen</th> 
    <th width="97">Student Info</th> 
    <th width="88">School App</th> 
    <th width="117">Professional Recomendation</th> 
    <th width="119">Reasonable Accomadations</th> 
    <th width="59">Drivers Licesnse</th> 
    <th width="91">High School Diploma</th> 
    </tr> 
<?php while ($row = mysql_fetch_array($ressel)) { ?> 
    <td width="146" height="50"><?php echo $row['fullname'];?></td> 
    <td width="17"><?php echo $row['entrancereadingscore'];?></td> 
    <td width="17"><?php echo $row['mathscore'];?></th> 
    <td width="17"><?php echo $row['locatinginfoscore'];?></td> 
    <td width="84"> <?php echo $row['backcalc'];?></td> 
    <td width="79"><?php echo $row['drugcalc'];?></td> 
    <td width="113"><?php if ($row['clearbackground']='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "no"; 
    } 
    ?></td> 
    <td width="114"> 
    <?php if ($row['cleardrugtest']=='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "No"; 
    } 
    ?> 
    </td> 
    <td width="96"> 
    <?php if ($row['studentinformationsheet']=='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "No"; 
    } 
    ?> 
    </td> 
    <td width="89"> 
    <?php if ($row['schoolapplication']=='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "No"; 
    } 
    ?> 
    </td> 
    <td width="118"> 
    <?php if ($row['professionalreco']=='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "No"; 
    } 
    ?> 
    </td> 
    <td width="119"> 
    <?php if ($row['reasonableaccom']=='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "No"; 
    } 
    ?> 
    </td> 
    <td width="58"> 
    <?php if ($row['driverlicesnce']=='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "No"; 
    } 
    ?> 
    </td> 
    <td width="91"> 
    <?php if ($row['highschooldip']=='1') 
    { 
     echo "Yes"; 
    } 
    else 
    { 
     echo "No"; 
    } 
    ?> 
    </td> 
    </tr> 
    <?php } ?> 
</table> 
</body> 
</html> 

回答

0

如果你想你的排序按id選擇框,然後肯定使用 「ORDER BY」 作爲Pachonk上述表示。

另外,我注意到,你的PHP如下:

  <option> 
      <? echo $options ?> 
     </option> 

的輸出:

<option> 
<option value="Some id">some text 
<option value="some other id">some other text 
... repeat for all options 
</option> 

您應該來自各地的PHP中刪除第一(你放置標籤在php回聲聲明),並將標籤放置在您的php代碼上面,以便每個都有一個

像:

$result = mysql_query($sql) or die (mysql_error()); 
    while ($row = mysql_fetch_array($result)) 
    { 
     $id=$row["id"]; 
      $course=$row["coursename"]; 
      $options.="<OPTION VALUE=\"$id\">".$course . "</option>"; 
                 ^^^^^^^^^^^^^ added these 
    } 
    ?> 

      // removed <option> 
      <? echo $options ?> 
      // removed </option> 
      </select> 

當然,你可以讓php中的while子句在那裏輸出你的選項,然後。

希望能以某種方式提供幫助。

+0

好吧,我做你建議的更改...(非常感謝)。不過我很好奇我怎麼能存儲選定值排序的下拉菜單下表...即..如果2013年1月AEMT課程選擇比我只需要從該類表中顯示的學生......我想這樣做與類 – 2013-03-08 15:00:42

+0

喜Joshua.The