sorting columns in a client-side page

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

What is an easy way to produce the headings for columns in an html table
that are sortable by clicking on them please?

I see these quite often and find them to be very useful, yet the code to
produce them is elusive for me. I am a beginner at all of php, mysql and
html and know nothing of css, and hope to find some coding examples that
are relatively easy to understand and incorporate into the existing
elementary-level code below.

Given a ./ that contains $username, $password, $database
and $table, the following is generic portable code that *should* work for
anybody to see the very simple kind of html tables that I'm currently
working with (my tables exclude the first field which is a unique auto-
increment key), but of course the "Chg" and "Del" radio buttons won't
work for anybody without the other scripts referenced in the onclick
events, so its perfectly safe for anyone to use on an existing database:


    or die( "Unable to connect to database");
    or die( "Unable to select database");
$querycols="SHOW COLUMNS IN $table;";
$queryrows="SELECT * FROM $table;";

echo "<b><center>Database Output: $numcols columns x $numrows rows</

// build the table column headers
echo "<table border=\"2\" cellspacing=\"2\" cellpadding=\"2\">\n<tr>
\n<th><font face=\"Arial, Helvetica, sans-serif\">Chg</font></th>
\n<th><font face=\"Arial, Helvetica, sans-serif\">Del</font></th>\n";
while ($i < $numcols) {
  $col_name[$i] = mysql_result($resultcols, $i, "Field");
  if ($i > 0) echo "<th><font face=\"Arial, Helvetica, sans-serif\">

// build the rows
while ($j < $numrows) {  //begin each row
   $id = mysql_result($resultrows, $j, "id");
   echo "<tr>\n";
   echo "<td align=center><input type=\"radio\" name=\"update\" value=
\"$id\" onclick=\"location.href='./update.php?ud_id='+this.value;\"</td>
   echo "<td align=center><INPUT TYPE=\"radio\" NAME=\"delete\" VALUE=
\"$id\" onclick=\"if (confirm('Delete this record?\nThis will be
permanent.')) \"</td>\n";
   while ($i < $numcols) {  // begin columns
     $item = mysql_result($resultrows, $j, "$col_name[$i]");
     echo "<td>$item&nbsp;</td>";
   }  //end of columns
   echo "\n</tr>\n";
}  // end of rows
echo "</table><br>\n";

Re: sorting columns in a client-side page

Greg Russell wrote:
Quoted text here. Click to load it

PHP cannot do this alone.  You need Javascript do either sort the
columns locally, or to make a call to the server to retrieve the
information in sorted order.

Try comp.lang.javascript.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: sorting columns in a client-side page

Quoted text here. Click to load it

client side table sort woud use some Javscript.

Here is one that works:


you code does not look very pretty.
I recommend the HEREDOC syntax. Find it on the website.

Re: sorting columns in a client-side page

Quoted text here. Click to load it

This approach is not intended to be a general mechanism that takes
a table it's never heard of before.  The page knows the columns and
how to label them.

I prefer to let the database do the sorting.  I typically define a
variable like 'sort', and the links that change the sort order for
the page mypage.php might look like mypage.php?sort=id or
mypage.php?sort=lastname .  Each keyword represents a sort order
(and not necessarily only on one column).  Keywords are arbitrary
but for ease of remembering what they do, they sort of describe the
ordering in one "word".

One of the steps in mypage.php is to look at the value of $_GET['sort']
or $_POST['sort'], depending on the form, and turn it into an ORDER
BY clause for a SQL statement in the variable $order, usually using
a big switch.  In order to avoid SQL injection, the value of
$_GET['sort'] must match one of a small number of legal values in
the switch, or if it doesn't, you get the default order, which might
be none or one of the other sort orders.  The final value of $order
is one of the constant values assigned to it in the code, and the
value of $_GET['sort'] selects which one.

    $order = '';            /* default */
    if (isset($_GET['sort'])) {
    $o = $_GET['sort'];
    switch($o) {
    case 'id':    $order = 'ORDER BY id desc'; break;
    case 'lastname': $order = 'ORDER BY lastname, firstname'; break;

Then when you get ready to do the query, you substitute $order
into the query in the right place.  

$query = "SELECT id, lastname, firstname FROM table $order";
Then run the query.

Since the query may involve joins of several tables, the value of $order
is closely tied to the query being done.

Re: sorting columns in a client-side page

On Dec 30, 4:42=A0am, (Gordon Burditt) wrote:
Quoted text here. Click to load it

If you are going to have multipage results (ie page 3 of 8) then you
will probably want to do all the sorting on the server side.
Bill H

Re: sorting columns in a client-side page

Quoted text here. Click to load it

Which means dynamically generating SQL, possibly using Ajax. And if
you need to ask you're probably already getting out of your depth.
There are AFAIK, no free packages to do this - but both phplens and
dhtmlxgrid will do most of the work for you and are reasonably well

Try Google for URLS, docs and downloads.


Site Timeline