Newsgroup: it.comp.www.html
Titolo: Table Sorting

Hi,
Does anyone have any ideas on how to sort a table by clicking the column.
i.e. a name column, and an age column - clicking the name will sort
alphabetically, the age numerically.
Using Dreamwever MX, mysql and PHP.
Cheers,
Darren

Darren wrote:

check this out:
"sorttable: Make all your tables sortable"
http://www.kryogenix.org/code/browser/sorttable/
-- 
email: goldicus1970 AT yahoo DOT it
blog: 
http://www.deelan.com/

 ha scritto nel messaggio
news:44mxb.743$HT2...@newsfep3-gui.server.ntli.net...

If the table is the result of a query on a db, it's simple.
Let's say the standard query is
select title, author from books
When you click one column (let's say 'title') of the table, you have to
reload the same page passing the parameter 'title', so that you will build a
new table, as result of the query
select title, author from books order by title
In practice you have a php page like this:
------ page.php -----
<?php
if ($column != "") $query = "select title, author  from books order by
$column;"
else $query = "select title, author  from books;"
<table>
<tr>
<td>
  <form action="page.php" method="post" name="frm1"><input type="hidden"
name="column" value="title"></form>
  <a href="#" onclick="document.frm1.submit();">Title</a>
</td>
<td>
  <form action="page.php" method="post" name="frm2"><input type="hidden"
name="column" value="author"></form>
  <a href="#" onclick="document.frm2.submit();">Title</a>
</td>
</tr>
<tr><td>...</td><td>...</td></tr> /* here execute query and build the table
*/
</table>
?>
-----------------------
I hope not to have made mistakes. I wrote it quickly and using cut&paste...
:-)
But I think the idea is clear.
Bye,
Ema


If you have a multidimensional array representing your table the problem 
  is to sort the array (should be the case of the query result).
I used this function, taken from the user contributed notes on the php 
manual, ref. array_multisort by Ichier (
www.ichier.de
) (14-Jun-2003 
10:00). imho it's a GREAT function, it helped me a lot, very simple to use.
here's  the cut'n'paste, hope it helps like helped me (for a working 
example write me)
Ciao
#######################################################################
ok, now i made it a real (prooved) function, cause i needed to sort by 
two of the columns and it didn't work, sorting twice...
is was kinda tricky, cause i never handled with eval in php, but now 
it's quite superb - ok i didn't any errorhandling, but who cares *g*
___________
the function:
function array_csort() {  //coded by Ichier2003
    $args = func_get_args();
    $marray = array_shift($args);
    $msortline = "return(array_multisort(";
    foreach ($args as $arg) {
        $i++;
        if (is_string($arg)) {
            foreach ($marray as $row) {
                $sortarr[$i][] = $row[$arg];
            }
        } else {
            $sortarr[$i] = $arg;
        }
        $msortline .= "\$sortarr[".$i."],";
    }
    $msortline .= "\$marray));";
    eval($msortline);
    return $marray;
}
the fine thing is, you can add as many parameters you need, and you can 
add sort_flags as well (but be shure not to put em into " or ' - took 
some time for me to get this *g*)
______
syntax:
$new_array = array_multisort($array [, 'col1' [, SORT_FLAG [, 
SORT_FLAG]]]...);
__________
explanation:
$array is the array you want to sort, 'col1' is the name of the column 
you want to sort, SORT_FLAGS are : SORT_ASC, SORT_DESC, SORT_REGULAR, 
SORT_NUMERIC, SORT_STRING
you can repeat the 'col',FLAG,FLAG, as often you want, the highest 
prioritiy is given to the first - so the array is sorted by the last 
given column first, then the one before ...
_______
example:
in my case the array has it's index as first dimension (i like that for 
using foreach later):
$array[0]['name'] = "Niko";
$array[0]['age'] = 24;
$array[0]['town'] = "Berlin";
$array[1]['name'] = "Dennis";
$array[1]['age'] = 34;
$array[1]['town'] = "Berlin";
$array[2]['name'] = "Oliver";
$array[2]['age'] = 44;
$array[2]['town'] = "Aachen";
$array[3]['name'] = "Andi";
$array[3]['age'] = 34;
$array[3]['town'] = "Berlin";
_____________________
use the function like this:
$array = array_csort($array,'town','age',SORT_DESC,'name');
here the resulting array would be:
$array[0]['name'] = "Oliver";
$array[0]['age'] = 44;
$array[0]['town'] = "Aachen";
$array[1]['name'] = "Andi";
$array[1]['age'] = 34;
$array[1]['town'] = "Berlin";
$array[2]['name'] = "Dennis";
$array[2]['age'] = 34;
$array[2]['town'] = "Berlin";
$array[3]['name'] = "Niko";
$array[3]['age'] = 24;
$array[3]['town'] = "Berlin";
that should be enough :)
.. waiting this fuc to be included to the next php-release *gg*
hope it helps you..Niko