Re: sorting columns in a client-side page



On 30 Dec, 10:26, Bill H <b...@xxxxxxxxx> wrote:
On Dec 30, 4:42 am, gordonb.o6...@xxxxxxxxxxx (Gordon Burditt) wrote:



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.

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.

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

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
documented.

Try Google for URLS, docs and downloads.

C.
.



Relevant Pages

  • Re: complex filter and calculations in access
    ... when using a subquery on the same table as the main query, ... switch to SQL view. ... of your database using the from address in this post. ...
    (microsoft.public.access.queries)
  • Re: design: single or multiple queries
    ... same query by varying the selection conditions and sort order. ... or move to MySQL or SQL Server. ... How does the query OrderBy property relate to the Sort specs shown ...
    (microsoft.public.access.queries)
  • RE: Error in SQL
    ... paste in the SQL statement, then switch back to the query grid. ... "jokobe" wrote: ...
    (microsoft.public.access.formscoding)
  • Re: Need help with initial design
    ... I copied your text, created a new query, switched to sql and pasted then ... Equip = EquipID, CustID, Make, Model, Serial ... View) then switch to Design Mode it will make much more sense. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Multiple-step_operation_generated_errors._Check_each_status_va
    ... Click to the Queries tab and create a new query in Design View ... Copy and paste this SQL Statement: ... switch back to SQL View without switching to Design View. ...
    (microsoft.public.scripting.vbscript)