制作下拉排序列表

时间:2013-03-08 03:12:04

标签: php mysql drop-down-menu

我有这个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>

1 个答案:

答案 0 :(得分:0)

如果您想按ID对选择框进行排序,请务必使用&#34; ORDER BY&#34;正如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 echo语句中放置一个标记)并将标记放在上面的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>

当然,你可以让while子句中的php在那里输出你的选项,然后也是。

希望在某种程度上有所帮助。