Re: Dynamic HTML table sort with PHP
| From: | tg-php at gryffyndevelopment dot com | Date: | Fri, 07 Jul 2006 17:01:03 +0000 |
| Subject: | Re: Dynamic HTML table sort with PHP | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-239197@lists.php.net to get a copy of this message | ||
If you're only sorting by one column at a time (not adding to a column sort list) then you can
do something like this:
$ascdesc = ($_GET['sortcol'] == 'colA' AND $_GET['ad'] ==
'ASC') ? 'DESC' : 'ASC';
echo "<a
href=\"script.php?sortcol=colA&ad=$ascdesc\">ColA</a>\n";
$ascdesc = ($_GET['sortcol'] == 'colB' AND $_GET['ad'] ==
'ASC') ? 'DESC' : 'ASC';
echo "<a
href=\"script.php?sortcol=colB&ad=$ascdesc\">ColA</a>\n";
$ascdesc = ($_GET['sortcol'] == 'colC' AND $_GET['ad'] ==
'ASC') ? 'DESC' : 'ASC';
echo "<a
href=\"script.php?sortcol=colC&ad=$ascdesc\">ColA</a>\n";
In this case, the only time you want a descending sort is when the column being sorted is the
current column (who's HREF is being output) and the current sort value is ASC. If current sort
value is blank, it'll default to ascending.
And if you're not familiar with that $var = (condition) ? 'value1' :
'value2'; syntax, look up ternary operations. It's just a short hand way of saying
"if this is true, use this value, else use this second value".
I also did code once where each column you clicked got added to the sort list. So if you clicked
column B, that became primary sort. If you clicked on column C after that, then C became the
primary sort and B was the secondary sort. On top of that I did thte ASC/DESC thing too.
Just a matter of working out the logic, but yes, you can do it all in PHP.
-TG
= = = Original message = = =
Good Day List,
Does anybody know of a pure PHP way to make table header cells trigger a
column sort?
If I have a table header that contains a link that when clicked
resubmits the page to itself with the proper sort query variables (e.g.
index.php?sortBy=ORDER_ID&sortOrder=ASC), I want PHP to rewrite the
sortOrder query string to be "sortOrder=DESC" and then "sortOrder=ASC"
the next time column heading is clicked. This way, the column will sort
like windows columns and users will feel "at-home" with the process.
I can write some JavaScript that will dynamically rewrite the href
attribute of the link, but I was hoping for a pure PHP solution in case
JavaScript is disabled.
Thanks.
Ken Vandegrift
kvandegrift@sharis.com <mailto:kvandegrift@sharis.com>
Web Administrator
Sharis Mgmt. Corp
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.