Forum Moderators: mack
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
[brainjar.com...]
Working demo:
[brainjar.com...]
Includes a full, nine page, detailed explanation. Looks just what you need.