Re: [PHP-WIN] Dynamic HTML table sort with PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx <mailto:kvandegrift@xxxxxxxxxx> 
Web Administrator
Sharis Mgmt. Corp


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux