tbone has asked for the wisdom of the Perl Monks concerning the following question:

I have created a table using HTML::Table from a database query. To allow sorting of the table, I created an href for each column name like href="pastRequests.pl?order=date" and use the parameter order in my query. This sorts in descending order. I want to be able to sort both ascending and descending. I have seen it done that if you click once, you get the list in ascending order, click it again you get it descending order. Does anyone know how I can do this? Thanks

Replies are listed 'Best First'.
Re: Resort Table
by tcf22 (Priest) on Oct 27, 2003 at 19:54 UTC
    I think this may be what you are looking for:
    my $sort_order = $q->param('sort_order'); my $order = $q->param('order'); print '<tr>'; foreach(@column_headings){ print "<td><A href=\"pastRequests.pl?order=$_&sort_order=" . (($_ eq $order && $sort_order eq 'desc') ? 'asc' : 'desc') . "\">$_</a></td>"; } print '</tr>'; my @records = ##Get your data, maybe an array of hashes?? @records = reverse(@records) if($sort_order eq 'asc'); foreach(@records){ ##Output records }

    - Tom

Re: Resort Table
by Abigail-II (Bishop) on Oct 27, 2003 at 19:26 UTC
    Well, just make the href pastRequests.pl?order=date;sortorder=ascending. If you then create a table sorted ascending, make the href above that column to be pastRequests.pl?order=date;sortorder=descending.

    Abigail

Re: Resort Table
by Zaxo (Archbishop) on Oct 27, 2003 at 19:30 UTC

    You can either call reverse or else gimmick the sort function by changing the order of comparison to {foo($b) <=> foo($a)} (or whatever).

    After Compline,
    Zaxo