Re: sorting columns in a client-side page



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.

.



Relevant Pages

  • 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: Copying a table to an array
    ... tests, with rather surprising results, referred to in my reply to Mike ... the code insists that it cannot find the query. ... I don't think that you can guarantee to maintain the sort order of the rows ... GetRows reads the table in row order. ...
    (microsoft.public.access.formscoding)
  • Re: Subform changes record order after edits
    ... Did you specify a sort order in the query? ... If you don't specify a sort order, ... >I have continuous form subform "sfrmProductComponents on a mainform ...
    (microsoft.public.access.formscoding)
  • Re: Last record search not working
    ... It may have been the compact and repair which ... and repair was always going to happen one day, ... lost the natural sort order and gave this problem. ... correct place in a complex set of subqueries: sometimes the query ...
    (microsoft.public.access.queries)
  • Re: sorting columns in a client-side page
    ...  Each keyword represents a sort order ... the switch, or if it doesn't, you get the default order, which might ... Then when you get ready to do the query, ...
    (comp.lang.php)