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

    After a bit more frustrated googling I finally found the answer at http://www.dougv.com/2009/06/13/sorting-your-mysql-results-set-in-php-using-jquery-and-a-more-traditional-approach/

    Here Dougv describes two approaches, one sorting the data on the server using PHP, which he describes as resource intensive and slow, and a second way using javascript which runs on the client side, and it works like a dream! You can see it working now on my web site via the link I gave in my previous post. You can click on a column heading to sort the table by that column, and then click again to reverse the sort order. Brilliant, just what I wanted.

    The only problem I have now is that the cursor changes to an "I" beam when it is hovering over the column heading, just like when it is hovering over any other piece of text. It should really change to a hand like for any other clickable link to give the user feedback. Oh well, nearly perfect.

    Regards - David

      David.B wrote:

      It should really change to a hand like for any other clickable link to give the user feedback.

      CSS has a rule for that: [font=monospace]cursor: pointer[/font].

        Weedpacket;10993380 wrote:

        CSS has a rule for that: [font=monospace]cursor: pointer[/font].

        Thanks, I hadn't thought of that. I just added style=\"cursor:pointer;\" to my table header row and the pointer works like it should. Great, thanks a lot.

        Regards - David

          2 years later
          Write a Reply...