Forum Moderators: mack

Message Too Old, No Replies

Sortable Tables

         

GhostPig

4:19 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



Hello all,

How would I go about setting up a table that can be sorted by clicking on the different headings?

So, if I had headings of title, score, date - clicking on each one would re-arrange the table according to which one I clicked on

Ta Muchly

DigitalSorceress

5:14 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



Well, there may be ways to do it with DHTML/XHTML pages where the data that you use to populate the table is in some XML format and you are using javascript to handle the sorting, but I'm no help with that.

I tend to do everything using MySql and PHP...

so here's the high-level logic to it:

<?
//RE-assign the value of $sortOrder to a value GUARANTEED not to break the
//sql query or allow for SQL injection attack
switch ($sortOrder) {
case BAR:
$sortOrder = "BAR";
break;
case BAZ:
$sortOrder = "BAZ";
break;
case QUX:
$sortOrder = "QUX";
break;
case QUUX:
$sortOrder = "QUUX";
break;
case QUUUX:
$sortOrder = "QUUUX";
break;
default:
$sortOrder = "FOO";
break;
}
$sql = "select FOO, BAR, BAZ, QUX, QUUX, QUUUX from SomeTable where QUUUUX='xyz' order by $sortOrder";

_yada yada, submit sql to sql server_
_we'll assume you have function called next_row() a single row from the result set will be returned as an associative array_
?>

<table>
<tr>
<th><a href="?sortOrder=FOO">FOO</a></th>
<th><a href="?sortOrder=BAR">BAR</a></th>
<th><a href="?sortOrder=BAZ">BAZ</a></th>
<th><a href="?sortOrder=QUX">QUX</a></th>
<th><a href="?sortOrder=QUUX">QUUX</a></th>
<th><a href="?sortOrder=QUUUX">QUUUX</a></th>
</tr>

<?
while($Row = next_row($query) {
echo "<tr>\n";
echo "<td>$Row[FOO]</td>\n";
echo "<td>$Row[BAR]</td>\n";
echo "<td>$Row[BAZ]</td>\n";
echo "<td>$Row[QUX]</td>\n";
echo "<td>$Row[QUUX]</td>\n";
echo "<td>$Row[QUUUX]</td>\n";
echo "</tr>\n";
}
?>

</table>

In the code above, notice how the first row (the header row) has the column names as links back to self with a sortOrder=COLUMN_NAME? well, when the page sees the request, it parses the sortOrder with a switch/case statement (for security) and then re-submits the query using the new value of sortOrder. (since I didn't specify ASC or DESC in the order by clause in the sql, they will all sort in ascending order. Using the same concepts, you can add an $ascDesc field...


<th><a href="?sortOrder=FOO&ascDesc=ASC">FOO</a><a href="?sortOrder=FOO&ascDesc=DESC">v</a></th>

and you'd need a similar switch/case to make the $ascDesc value safe to pass to the query and set the default to ASC if it isn't passed

encyclo

5:19 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than having to buy a commercial script, you can try this superb example, which you can implement for free ;)

[brainjar.com...]

Working demo:

[brainjar.com...]

Includes a full, nine page, detailed explanation. Looks just what you need.

GhostPig

7:35 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



Thanks encyclo - that's just what I needed!

mack

11:51 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



There is a great related thread here...
[webmasterworld.com...]

Mack.