0

我有这个 php 代码,其中包含从数据库中提取信息的下拉菜单,但似乎无法弄清楚如何让下拉选择对列表进行排序...

下拉菜单将课程名称显示为标签,但选择时需要按课程 ID 对列表进行排序。

访问此处查看实际操作中的表格

<?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>
4

1 回答 1

0

如果您想按 id 对选择框进行排序,那么一定要使用 Pachonk 上面所说的“ORDER BY”。

另外,我注意到您的 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 立即输出您的选项,然后也输出。

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

于 2013-03-08T04:43:01.373 回答