[英]SQL ORDER BY / Sort Php
I am struggling with my php and would love some assistance in the right direction. 我在用PHP挣扎,希望在正确的方向上提供一些帮助。 Here is my code so far: 到目前为止,这是我的代码:
<?php
$connection = mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("rsi", $connection) or die(mysql_error());
$query = "SELECT * FROM events";
$result = mysql_query ($query) or die ("error in query");
if (mysql_num_rows($result)>0) {
echo "<table border=1></tr>" .
"<th>ID</th>" .
"<th>Name</th>" .
"<th>Date</th>" .
"<th>Location</th>" ;
while ($row = @ mysql_fetch_array($result)){
print "<tr>";
print "<td>".$row['id']."</td>";
print "<td>".$row['name']."</td>";
print "<td>".$row['date']."</td>";
print "<td>".$row['location']."</td>";
print "</tr>";
}
print "</table>";
}
?>
I am now hoping to order the results from the database by clicking on each/any of the headers (for example date). 我现在希望通过单击每个/任何标题(例如日期)来从数据库中排序结果。 Thanks for your time. 谢谢你的时间。
I think you are looking to sort the table in the browser. 我认为您正在寻找在浏览器中对表格进行排序的方法。 You probably don't want to keep querying your database for the same information just in a different sort order. 您可能不希望仅以不同的排序顺序就数据库查询相同的信息。 Something like this perhaps: http://tablesorter.com/docs/#Demo 可能是这样的: http : //tablesorter.com/docs/#Demo
To use tablesorter: 要使用表排序器:
echo "<table border=1 id="myTable" class="tablesorter"><tr>" ."<th>ID</th>" ."<th>Name</th>" ."<th>Date</th>" ."<th>Location</th></tr>" ;
<script> $(document).ready(function() { $("#myTable").tablesorter(); } ); </script>
<Head>
section: 如果还没有jquery,请将其包含在<Head>
部分中: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Download jquery.tablesorter.js from http://tablesorter.com/__jquery.tablesorter.zip 从http://tablesorter.com/__jquery.tablesorter.zip下载jquery.tablesorter.js
Include the file jquery.tablesorter.js like this: 像这样包含文件jquery.tablesorter.js:
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
To order the result you use ORDER BY
. 要订购结果,请使用ORDER BY
。
Here's an example to order by id: 以下是按ID排序的示例:
$query = "SELECT * FROM events ORDER BY id";
To get it to order when you click on a header you need to make it execute the new query in some way. 要在单击标题时对其进行排序,需要使其以某种方式执行新查询。 How to do that I leave out for you to find. 该如何做,我留给您查找。 Typically you reload the whole page with a parameter on what to sort on or use ajax, which would let you update contents on the page without reloading the page. 通常,您使用要排序的内容或使用ajax的参数重新加载整个页面,这将使您无需重新加载页面即可更新页面上的内容。
You have to pass the order by column name and order ascending or decending by either GET or POST. 您必须按列名传递订单,并按GET或POST升序或降序。 Here is an example of GET. 这是GET的示例。
$query = "SELECT * FROM events";
if(isset($_GET['sort'])
{
$query.= ' order by '.mysql_real_escape_string($_GET['sort']).' '.mysql_real_escape_string($_GET['order']);
}
$result = mysql_query ($query) or die ("error in query");
while ($row = @ mysql_fetch_array($result)){
print "<tr>";
print "<td>".$row['id']."</td>";
print "<td>".$row['name']."</td>";
print "<td><a href='http://www.domain.com/currentpageurl?sort=date&order=asc'>".$row['date']."</td>";
print "<td>".$row['location']."</td>";
print "</tr>";
}
If you use Shakti's answer, I would recommend not using $_GET directly in the query. 如果使用Shakti的答案,建议不要在查询中直接使用$ _GET。 I would run your $_GET through some sort of sanity check and only allow specific values, otherwise you are leaving your site open to SQL injection attacks. 我将通过某种健全性检查来运行$ _GET,并且只允许使用特定值,否则,您的站点将遭受SQL注入攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.