I needed to display a table on my web site so I have learnt just enough PHP and MySQL to do this, but I would like to be able to sort it by clicking on the column headings. I know this has probably been asked a thousand times, but googling I don't find anything that I either understand or seems to work, so I would appreciate being pointed in the right direction please.
The page I am working on can be found here: http://www.vintagewatchstraps.com/borgelcasenumbers.php
And the PHP code I have used to build the table on the page is:
<?php
mysql_connect("cust-mysql-123-03", $username, $password)or die("Unable to connect to MySQL server") ;
mysql_select_db("vintagewatchstrapsc_653242_db1") or die( "Unable to select database" ) ;
$data = mysql_query("SELECT * FROM borgelcasenumbers ORDER BY MainSerialNo") or die( "Unable to get data" );
Print "<table class='fine'>";
Print "<thead><tr align='center'>
<th>Material</th>
<th>Sponsor</th>
<th>Office</th>
<th>Date<br />Letter</th>
<th>Year</th>
<th>Main<br />Serial<br />Number</th>
<th>Sub-Main<br />Serial<br />Number</th>
<th>Sub<br />Serial<br />Number</th>
<th>Sub-Sub<br />Serial<br />Number</th>
<th>Movement</th>
<th>Notes</th>
<th>Contributor</th></tr></thead>";
Print "<tbody>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr align='center'>";
Print "<td>".$info['Material'] . "</td> ";
Print "<td>".$info['Sponsor'] . " </td>";
Print "<td>".$info['Office'] . " </td>";
Print "<td>".$info['DateLetter'] . " </td>";
Print "<td>".$info['Year'] . " </td>";
Print "<td>".$info['MainSerialNo'] . " </td>";
Print "<td>".$info['SubMainSerialNo'] . " </td>";
Print "<td>".$info['SubSerialNo'] . " </td>";
Print "<td>".$info['SubSubSerialNo'] . " </td>";
Print "<td>".$info['Movement'] . " </td>";
Print "<td>".$info['Notes1'] . " </td>";
Print "<td>".$info['Contributor'] . " </td>";
Print "</tr>";
}
Print "</tbody>";
Print "</table>";
?>
I know that I could do what I want by making each column heading a clickable link to a new page with a different ORDER BY statement, but I really want to do the sorting without reloading the page. And I would like to be able to change the sort order by clicking on the column heading again.
From googling I get the idea that a javascript function might do the trick, but I don't know whether this will work in my case because the table is built dynamically from data in the MySQL database, rather than being hard coded into the page.
Any help gratefully received!
Regards - David