使用 order by 子句和 switch 语句尝试根据从下拉菜单中选择的值对表进行排序
我想使用下拉列表来对查询结果进行排序。无论在下拉列表中选择什么选项,查询都将按所选选项的升序排列。我已经快到了,但问题是,无论我从下拉菜单中选择哪个选项,它都仅按会话 ID 对表进行排序。如何让其他下拉值起作用,以便在选择它们时,表根据所选值进行排序。请仔细查看查询中的表单、switchstatment 和 ORDER BY 子句,请帮助我解决这个问题。
以下是代码:
<!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>
<title>Exam Interface</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="exam_interface.php" method="post" name="sessionform"> <!-- This will post the form to its own page"-->
<p>Session ID: <input type="text" name="sessionid" /></p> <!-- Enter Session Id here-->
<p>Module Number: <input type="text" name="moduleid" /></p> <!-- Enter Module Id here-->
<p>Teacher Username: <input type="text" name="teacherid" /></p> <!-- Enter Teacher here-->
<p>Student Username: <input type="text" name="studentid" /></p> <!-- Enter User Id here-->
<p>Grade: <input type="text" name="grade" /></p> <!-- Enter Grade here-->
<p>Order Results By: <select name="order">
<option value="noorder">Don't Order Results</option>
<option value="ordersessionid">Session ID</option>
<option value="ordermoduleid">Module Number</option>
<option value="orderteacherid">Teacher Username</option>
<option value="orderstudentid">Student Username</option>
<option value="ordergrade">Grade</option>
</select>
<p><input type="submit" value="Submit" /></p>
</form>
<?php
$username="xxx";
$password="xxx";
$database="mobile_app";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$sessionid = isset ($_POST['sessionid']) ? $_POST['sessionid'] : "";
$moduleid = isset ($_POST['moduleid']) ? $_POST['moduleid'] : "";
$teacherid = isset ($_POST['teacherid']) ? $_POST['teacherid'] : "";
$studentid = isset ($_POST['studentid']) ? $_POST['studentid'] : "";
$grade = isset ($_POST['grade']) ? $_POST['grade'] : "";
$orderfield = isset ($_POST['order']) ? $_POST['order'] : 'default order field';
$sessionid = mysql_real_escape_string($sessionid);
$moduleid = mysql_real_escape_string($moduleid);
$teacherid = mysql_real_escape_string($teacherid);
$studentid = mysql_real_escape_string($studentid);
$grade = mysql_real_escape_string($grade);
$_POST['ordersessionid'] = 'gr.SessionId';
$orderfield = isset ($_POST['order']);
switch ($orderfield) {
case 'ordersessionid': $orderfield = 'gr.SessionId';
break;
case 'ordermoduleid': $orderfield = 'm.ModuleId';
break;
case 'orderteacherid': $orderfield = 's.TeacherId';
break;
case 'orderstudentid': $orderfield = 'gr.StudentId';
break;
case 'ordergrade': $orderfield = 'gr.Grade';
break;
}
echo $orderfield;
$result = mysql_query("SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE ('$sessionid' = '' OR gr.SessionId = '$sessionid') AND ('$moduleid' = '' OR m.ModuleId = '$moduleid') AND ('$teacherid' = '' OR s.TeacherId = '$teacherid') AND ('$studentid' = '' OR gr.StudentId = '$studentid') AND ('$grade' = '' OR gr.Grade = '$grade') ORDER BY {$orderfield} ASC");
$num=mysql_numrows($result);
echo "<table border='1'>
<tr>
<th>Student Id</th>
<th>Forename</th>
<th>Session Id</th>
<th>Grade</th>
<th>Mark</th>
<th>Module</th>
<th>Teacher</th>
</tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['StudentId'] . "</td>";
echo "<td>" . $row['Forename'] . "</td>";
echo "<td>" . $row['SessionId'] . "</td>";
echo "<td>" . $row['Grade'] . "</td>";
echo "<td>" . $row['Mark'] . "</td>";
echo "<td>" . $row['ModuleName'] . "</td>";
echo "<td>" . $row['TeacherId'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
</body>
</html>
非常感谢,请帮忙。
I want to use a dropdown list to order the query result. Whatever option is chosen in the dropdown list, the query will be ordered in ascending order by the option selected. I am almost there but problem is that no matter which otion I choose from the drop down menu, it orders the table by Session ID only. How can I get the other drop down values to work so that when they are selected the table is ordered depending on the value chosen. Please look at the form, switchstatment and ORDER BY clause in query carefully and please help me on this problem.
Below is the code:
<!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>
<title>Exam Interface</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="exam_interface.php" method="post" name="sessionform"> <!-- This will post the form to its own page"-->
<p>Session ID: <input type="text" name="sessionid" /></p> <!-- Enter Session Id here-->
<p>Module Number: <input type="text" name="moduleid" /></p> <!-- Enter Module Id here-->
<p>Teacher Username: <input type="text" name="teacherid" /></p> <!-- Enter Teacher here-->
<p>Student Username: <input type="text" name="studentid" /></p> <!-- Enter User Id here-->
<p>Grade: <input type="text" name="grade" /></p> <!-- Enter Grade here-->
<p>Order Results By: <select name="order">
<option value="noorder">Don't Order Results</option>
<option value="ordersessionid">Session ID</option>
<option value="ordermoduleid">Module Number</option>
<option value="orderteacherid">Teacher Username</option>
<option value="orderstudentid">Student Username</option>
<option value="ordergrade">Grade</option>
</select>
<p><input type="submit" value="Submit" /></p>
</form>
<?php
$username="xxx";
$password="xxx";
$database="mobile_app";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$sessionid = isset ($_POST['sessionid']) ? $_POST['sessionid'] : "";
$moduleid = isset ($_POST['moduleid']) ? $_POST['moduleid'] : "";
$teacherid = isset ($_POST['teacherid']) ? $_POST['teacherid'] : "";
$studentid = isset ($_POST['studentid']) ? $_POST['studentid'] : "";
$grade = isset ($_POST['grade']) ? $_POST['grade'] : "";
$orderfield = isset ($_POST['order']) ? $_POST['order'] : 'default order field';
$sessionid = mysql_real_escape_string($sessionid);
$moduleid = mysql_real_escape_string($moduleid);
$teacherid = mysql_real_escape_string($teacherid);
$studentid = mysql_real_escape_string($studentid);
$grade = mysql_real_escape_string($grade);
$_POST['ordersessionid'] = 'gr.SessionId';
$orderfield = isset ($_POST['order']);
switch ($orderfield) {
case 'ordersessionid': $orderfield = 'gr.SessionId';
break;
case 'ordermoduleid': $orderfield = 'm.ModuleId';
break;
case 'orderteacherid': $orderfield = 's.TeacherId';
break;
case 'orderstudentid': $orderfield = 'gr.StudentId';
break;
case 'ordergrade': $orderfield = 'gr.Grade';
break;
}
echo $orderfield;
$result = mysql_query("SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE ('$sessionid' = '' OR gr.SessionId = '$sessionid') AND ('$moduleid' = '' OR m.ModuleId = '$moduleid') AND ('$teacherid' = '' OR s.TeacherId = '$teacherid') AND ('$studentid' = '' OR gr.StudentId = '$studentid') AND ('$grade' = '' OR gr.Grade = '$grade') ORDER BY {$orderfield} ASC");
$num=mysql_numrows($result);
echo "<table border='1'>
<tr>
<th>Student Id</th>
<th>Forename</th>
<th>Session Id</th>
<th>Grade</th>
<th>Mark</th>
<th>Module</th>
<th>Teacher</th>
</tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['StudentId'] . "</td>";
echo "<td>" . $row['Forename'] . "</td>";
echo "<td>" . $row['SessionId'] . "</td>";
echo "<td>" . $row['Grade'] . "</td>";
echo "<td>" . $row['Mark'] . "</td>";
echo "<td>" . $row['ModuleName'] . "</td>";
echo "<td>" . $row['TeacherId'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
</body>
</html>
Thank You very much and please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发生这种情况是因为您将
$orderfield
定义为isset ($_POST['order'])
。 isset() 返回一个布尔值;它不会返回其参数的值,即使用作其参数的变量实际上已定义。如果您将该行更改为看起来更像涉及isset()
的其他行,您将使其正常工作。为了安全起见,我建议执行两个额外步骤:
default
情况,以防提交的order
字段不是您的预期值之一This happens because you define
$orderfield
to beisset ($_POST['order'])
.isset()
returns a boolean value; it doesn't return the value of its argument, not even if the variable used as its argument is in fact defined. If you change that line to look more like the other lines involvingisset()
, you will get it to work.For security, I recommend two additional steps:
default
case to your switch in case the submittedorder
field isn't one of your expected values这行是你的问题:
你正在将要打开的变量设置为 true 或 false。
This line is your problem:
You are setting the variable you are switching on, to be true or false.
您仅因为您的代码而面临某种问题..:)
只需尝试此代码..我认为您会摆脱问题。
谢谢。
you are facing some kind of problem because of your code only.. :)
Just try this code..i think you will get out off problem.
Thanks.