PHP - 如何按升序或降序排列数据
PHP - How to order data by ascending or descending
我有一个允许用户从数据库中检索信息的表单。
目前用户可以使用我提供的代码从数据库中检索信息(可能是糟糕的编码但它有效)。
现在我希望数据根据用户在表单上的选择以 asc 或 dsc 顺序显示。但我不太确定如何去做,任何在正确方向上的帮助将不胜感激!
检索信息的PHP:
$sql = "SELECT RunnerID, EventID, Date, FinishTime, Position, CategoryID, AgeGrade, PB FROM Results";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>RunnerID</th><th>EventID</th><th>Date</th><th>FinishTime</th><th>Position</th><th>CategoryID</th><th>AgeGrade</th><th>PB</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["RunnerID"]. "</td><td>" . $row["EventID"]. " </td><td>" . $row["Date"]. " </td><td>" . $row["FinishTime"]. " </td><td>" . $row["Position"]. " </td><td>" . $row["CategoryID"]. " </td><td>" . $row["AgeGrade"]. " </td><td>" . $row["PB"]. " </td></tr>";
}
echo "</table>";
}
else {
echo "Error";
}
与其让 PHP 完成这项工作,更标准的做法是修改您的 SQL 查询以包含 ORDER BY xxxx ASC
或 ORDER BY xxxx DESC
。如果你有一个你想让他们排序的表格,一个不错的选择可能是这样的:
$userSelectedProperty = $_GET['ThePropertyIWantToSortOn'];
$userSelectedDirection = $_GET['TheDirectionIWantToSortBy'];
$sql = "SELECT RunnerID, EventID ... FROM Results ORDER BY "; // Note the extra space
switch ($userSelectedProperty)
{
case 'name': { $sql .= "RunnerName"; break; }
case 'age': { $sql .= "AgeGrade"; break; }
...
default: { $sql .= "RunnerID"; break; } // By default, let's sort by ID
}
if ($userSelectedDirection == 'desc')
{
$sql .= " DESC"; // Note the preceding space.
}
else
{
$sql .= " ASC"; // Note the preceding space.
}
这样,排序就已经在数据库上完成了,所以您已经按照您想要的顺序检索数据了。我使用 switch 语句和 if 语句的原因,即使它看起来是相同的数据,是因为它避免了一个称为 SQL 注入 的漏洞。基本上,如果您只是直接使用该变量来构建 SQL 查询,就无法阻止一些恶意用户提供允许他们修改查询意图的代码。例如,如果他们传入类似 MyName; DELETE FROM Results;
的内容作为用于生成字段 $userSelectedProperty
的表单输入的值,则生成的 $sql
字符串将类似于 SELECT RunnerID ... FROM Results ORDER BY MyName; DELETE FROM Results;
。嗯,这 真的很糟糕 因为 SQL,这是两个有效的陈述。如果这是您第一次听说它,这不是最好或最全面的定义,但如果您对如何避免它感到好奇,我建议您查看 SQL 注入指南。
$fields = array( "RunnerID", "EventID", "Date", "FinishTime", "Position", "CategoryID" );
$orderby = 0;
$asc = 0;
if( isset($_GET['orderby'])) $orderby = (int)$_GET['orderby'];
if( isset($_GET['asc'])) $asc = (int)$_GET['asc'];
$sql = "SELECT RunnerID, EventID, Date, FinishTime, Position, CategoryID, AgeGrade, PB FROM Results";
$sql .= " ORDER BY " . $fields[$orderby];
$sql .= " " . $asc ? "ASC" : "DESC";
与其让 服务器 做这些工作,不如考虑让 浏览器 做。将工作卸载到浏览器是一项非常有用的技能,尤其是对于非常繁忙的网站。
将数据放入 table 中,而不考虑排序顺序。然后使用JavaScript实现排序。有很多方法可以做到这一点,例如:
var tbl = document.getElementById('mytable'),
trs = tbl.rows, l = trs.length, i, tmp = [];
for( i=0; i<l; i++) tmp.push(trs[i]);
tmp.sort(function(a,b) {
// compare the rows how you want.
// return -1 if a comes before b
// return 1 if b comes before a
// return 0 if they are equal
});
for( i=0; i<l; i++) tbl.appendChild(tmp[i]);
您还可以查看 Internet 上的众多插件之一来执行此操作,例如 Footable。
当然,您始终可以通过以下方式支持非 JS 用户(他们有多过时?):
<noscript><a href="?sort=asc">Sort ascending</a></noscript>
当该参数存在时,按照其他答案对它们进行服务器端排序。不过,绝大多数情况下,它将使用 JavaScript.
完成
我有一个允许用户从数据库中检索信息的表单。
目前用户可以使用我提供的代码从数据库中检索信息(可能是糟糕的编码但它有效)。
现在我希望数据根据用户在表单上的选择以 asc 或 dsc 顺序显示。但我不太确定如何去做,任何在正确方向上的帮助将不胜感激!
检索信息的PHP:
$sql = "SELECT RunnerID, EventID, Date, FinishTime, Position, CategoryID, AgeGrade, PB FROM Results";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>RunnerID</th><th>EventID</th><th>Date</th><th>FinishTime</th><th>Position</th><th>CategoryID</th><th>AgeGrade</th><th>PB</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["RunnerID"]. "</td><td>" . $row["EventID"]. " </td><td>" . $row["Date"]. " </td><td>" . $row["FinishTime"]. " </td><td>" . $row["Position"]. " </td><td>" . $row["CategoryID"]. " </td><td>" . $row["AgeGrade"]. " </td><td>" . $row["PB"]. " </td></tr>";
}
echo "</table>";
}
else {
echo "Error";
}
与其让 PHP 完成这项工作,更标准的做法是修改您的 SQL 查询以包含 ORDER BY xxxx ASC
或 ORDER BY xxxx DESC
。如果你有一个你想让他们排序的表格,一个不错的选择可能是这样的:
$userSelectedProperty = $_GET['ThePropertyIWantToSortOn'];
$userSelectedDirection = $_GET['TheDirectionIWantToSortBy'];
$sql = "SELECT RunnerID, EventID ... FROM Results ORDER BY "; // Note the extra space
switch ($userSelectedProperty)
{
case 'name': { $sql .= "RunnerName"; break; }
case 'age': { $sql .= "AgeGrade"; break; }
...
default: { $sql .= "RunnerID"; break; } // By default, let's sort by ID
}
if ($userSelectedDirection == 'desc')
{
$sql .= " DESC"; // Note the preceding space.
}
else
{
$sql .= " ASC"; // Note the preceding space.
}
这样,排序就已经在数据库上完成了,所以您已经按照您想要的顺序检索数据了。我使用 switch 语句和 if 语句的原因,即使它看起来是相同的数据,是因为它避免了一个称为 SQL 注入 的漏洞。基本上,如果您只是直接使用该变量来构建 SQL 查询,就无法阻止一些恶意用户提供允许他们修改查询意图的代码。例如,如果他们传入类似 MyName; DELETE FROM Results;
的内容作为用于生成字段 $userSelectedProperty
的表单输入的值,则生成的 $sql
字符串将类似于 SELECT RunnerID ... FROM Results ORDER BY MyName; DELETE FROM Results;
。嗯,这 真的很糟糕 因为 SQL,这是两个有效的陈述。如果这是您第一次听说它,这不是最好或最全面的定义,但如果您对如何避免它感到好奇,我建议您查看 SQL 注入指南。
$fields = array( "RunnerID", "EventID", "Date", "FinishTime", "Position", "CategoryID" );
$orderby = 0;
$asc = 0;
if( isset($_GET['orderby'])) $orderby = (int)$_GET['orderby'];
if( isset($_GET['asc'])) $asc = (int)$_GET['asc'];
$sql = "SELECT RunnerID, EventID, Date, FinishTime, Position, CategoryID, AgeGrade, PB FROM Results";
$sql .= " ORDER BY " . $fields[$orderby];
$sql .= " " . $asc ? "ASC" : "DESC";
与其让 服务器 做这些工作,不如考虑让 浏览器 做。将工作卸载到浏览器是一项非常有用的技能,尤其是对于非常繁忙的网站。
将数据放入 table 中,而不考虑排序顺序。然后使用JavaScript实现排序。有很多方法可以做到这一点,例如:
var tbl = document.getElementById('mytable'),
trs = tbl.rows, l = trs.length, i, tmp = [];
for( i=0; i<l; i++) tmp.push(trs[i]);
tmp.sort(function(a,b) {
// compare the rows how you want.
// return -1 if a comes before b
// return 1 if b comes before a
// return 0 if they are equal
});
for( i=0; i<l; i++) tbl.appendChild(tmp[i]);
您还可以查看 Internet 上的众多插件之一来执行此操作,例如 Footable。
当然,您始终可以通过以下方式支持非 JS 用户(他们有多过时?):
<noscript><a href="?sort=asc">Sort ascending</a></noscript>
当该参数存在时,按照其他答案对它们进行服务器端排序。不过,绝大多数情况下,它将使用 JavaScript.
完成