I'm having problems sorting my columns in an HTML table that I made from a mySQL database:
<?php
mysql_connect( 'localhost', 'user', 'pass' ) or die ( 'Unable to
connect.' );
mysql_select_db( 'robotdb' ) or die ( 'Unable to select database.' );
// if $sortby isn't empty, then check to see which column we're sorting by
if( $sortby != "" ){
switch( $sortby ){
case "col1": $sql = "SELECT FROM WTL ORDER BY PlayerID"; //
sorts by column1
case "col2": $sql = "SELECT FROM WTL ORDER BY Name"; //
etc.
case "col3": $sql = "SELECT FROM WTL ORDER BY Completions";
// etc.
default: $sql = "SELECT FROM WTL"; // if $sortby isn't one
of our values don't sort
}
}
// otherwise, don't sort
else {
$sql = "SELECT * FROM WTL";
}
$result = mysql_query( $sql ) or die ( 'Unable to execute query.' );
?>
<table border="1">
<tr>
<th><a href="WTLstats1.php?sortby=col1">Player ID</a></th>
<th><a href="WTLstats1.php?sortby=col2">Name</a></th>
<th><a href="WTLstats1.php?sortby=col3">Completions</a></th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?=$row[0]?></td>
<td><?=$row[1]?></td>
<td><?=$row[2]?></td>
</tr>
<?php
}
?>
</table>