<?php
$sql = "select * from sometable";
switch ( $_REQUEST['orderby'] ) {
case "name": $sql .= " order by name"; break;
case "blah": $sql .= " order by blah"; break;
default: $sql .= " order by whatevercolumnyouwantasdefault";
}
...
?>
或者甚至像这样简单,假设您已经检查了 $_REQUEST 值并确定它们是合法的:
<?php
$sql = "select * from sometable";
if ( isset($_REQUEST['orderby']) )
$sql .= " order by ".$_REQUEST['orderby'];
else
$sql .= " order by whatevercolumnyouwantasdefault";
?>
I would probably do this with some sort of client-side script, I know there are several JavaScript or DHTML options out there. These would be executed much more quickly for your users (on most machines, anyway) and avoid unnecessary hits to the database. I think the last one I looked at was this: http://www.kryogenix.org/code/browser/sorttable/
But if you're really interested in doing it with your SQL query...so you have your table with results, something similar to:
Then when constructing the query, you could simply do something like:
<?php
$sql = "select * from sometable";
switch ( $_REQUEST['orderby'] ) {
case "name": $sql .= " order by name"; break;
case "blah": $sql .= " order by blah"; break;
default: $sql .= " order by whatevercolumnyouwantasdefault";
}
...
?>
Or even something as simple as this, assuming you've checked the $_REQUEST values and determined they are legitimate:
<?php
$sql = "select * from sometable";
if ( isset($_REQUEST['orderby']) )
$sql .= " order by ".$_REQUEST['orderby'];
else
$sql .= " order by whatevercolumnyouwantasdefault";
?>
发布评论
评论(2)
我可能会使用某种客户端脚本来完成此操作,我知道有多种 JavaScript 或 DHTML 选项。这些将为您的用户更快地执行(无论如何在大多数机器上),并避免对数据库不必要的点击。我想我最后看的是这个: http://www.kryogenix.org/ code/browser/sorttable/
但是如果你真的有兴趣用你的 SQL 查询来做这件事...所以你的表中有结果,类似于:
然后在构建查询时,你可以简单地做一些事情就像:
或者甚至像这样简单,假设您已经检查了 $_REQUEST 值并确定它们是合法的:
I would probably do this with some sort of client-side script, I know there are several JavaScript or DHTML options out there. These would be executed much more quickly for your users (on most machines, anyway) and avoid unnecessary hits to the database. I think the last one I looked at was this: http://www.kryogenix.org/code/browser/sorttable/
But if you're really interested in doing it with your SQL query...so you have your table with results, something similar to:
Then when constructing the query, you could simply do something like:
Or even something as simple as this, assuming you've checked the $_REQUEST values and determined they are legitimate:
也许您可以将结果存储在某种数组中。
编辑:
否则,您可以使用 JavaScript 对表进行排序,而无需刷新页面。这是一个 jQuery 插件,可以为您完成此操作。
Perhaps you could store the results in some sort of array.
EDIT:
Otherwise you can use JavaScript to sort the table without having to refresh the page. Here is a jQuery plugin that can do this for you.