简体   繁体   English

制作下拉排序列表

[英]Make dropdown sort list

I have this php code with a drop down pulling info from the database but cannot seem to figure out how to get the drop down selection to sort the list... 我有一个带有从数据库中提取信息的下拉列表的php代码,但似乎无法弄清楚如何获取下拉列表来对列表进行排序...

The drop down is showing the course name as the label but when selected needs to sort the list by course id. 下拉列表将课程名称显示为标签,但选择后需要按课程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>

If you want to sort your select box by id, then definitely use "ORDER BY" as Pachonk says above. 如果您想按ID对选择框进行排序,则一定要使用Pachonk上面所说的“ ORDER BY”。

Also, I've noticed that your PHP below: 另外,我注意到您的PHP如下:

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

is outputting: 输出:

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

You should remove the first from around the PHP (you are placing an tag in the php echo statement anyway) and place the tag in your php code above so that every has an 您应该从PHP周围删除第一个标记(无论如何,您都将标记放置在php echo语句中),并将标记放置在上方的php代码中,以便每个标记都有一个

like: 喜欢:

$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>

Of course, you could just have the php in the while clause output your option right there and then too. 当然,您可以只在while子句中将php输出到那里,然后再输出您的选项。

Hope that helps in some way. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM