Page 1 of 1

Sortable Table

Posted: Sun Sep 14, 2003 4:02 pm
by Nick Ewing
Hello, I'm trying to make a table that that when you sort one colum it keeps the other colums in the right place. So it would be something like this:

before sorting:
a b c
3 1 2
1 2 3
2 3 1

after sorting the a colume:
a b c
1 2 3
2 3 1
3 1 2

Posted: Sun Sep 14, 2003 4:04 pm
by m3rajk
try looking at array_sort()

Posted: Sun Sep 14, 2003 4:23 pm
by JAM
There is no such thing (array_sort).
http://se.php.net/manual/en/function.ar ... tisort.php or http://se.php.net/manual/en/function.sort.php might be worth looking into.

But you do not mention how you are building this table, and by table do you meant a <table> as in html?

the table

Posted: Sun Sep 14, 2003 4:30 pm
by Nick Ewing
well i want to accomplsh something like what apache does with directories

example:
http://nickewing.gotdns.com/pictures/

So when i click the Name colum it sorts the name colum and puts the other information in the right place

Posted: Sun Sep 14, 2003 4:59 pm
by JAM
Found this on php.net, as sorting multidimensional arrays has not got a function (what I know of):

Code: Select all

<?php
$array[1]['name'] = 'A';
$array[2]['name'] = 'B';
$array[3]['name'] = 'C';
$array[1]['size'] = 'C';
$array[2]['size'] = 'B';
$array[3]['size'] = 'A';
$array[1]['type'] = 'C';
$array[2]['type'] = 'A';
$array[3]['type'] = 'B';

// function found on php.net
function array_csort() {  //coded by Ichier2003
    $args = func_get_args();
    $marray = array_shift($args);
    $i = 0;
    $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;
}

// lets test it
$test1 = array_csort($array,'size');
print_r($test1);
$test2 = array_csort($array,'type');
print_r($test2);
$test3 = array_csort($array,'name','type');
print_r($test3);
?>
Hope you can make some use of this. Supports multible sorting also, by adding more fields as in the last example in the code.

Posted: Sun Sep 14, 2003 6:25 pm
by Nick Ewing
thanks but im rather new to this and am having problems working with that.

Re: the table

Posted: Sun Sep 14, 2003 6:27 pm
by Nay
Nick Ewing wrote:well i want to accomplsh something like what apache does with directories

example:
http://nickewing.gotdns.com/pictures/

So when i click the Name colum it sorts the name colum and puts the other information in the right place
You mean a type of directory viewer? Maybe open_dir() was what he was looking for, guys. Search for open_dir() at php.net. It might be what you're looking for.

Hope that helps...

-Nay

Posted: Mon Sep 15, 2003 2:28 pm
by m3rajk
JAM wrote:There is no such thing (array_sort).
http://se.php.net/manual/en/function.ar ... tisort.php or http://se.php.net/manual/en/function.sort.php might be worth looking into.

But you do not mention how you are building this table, and by table do you meant a <table> as in html?
thanx jam. i was thinking array_multisort. it's meant to do multiple sorting types upon multiple types of arrays.
Nick Ewing wrote:well i want to accomplsh something like what apache does with directories

example:
http://nickewing.gotdns.com/pictures/

So when i click the Name colum it sorts the name colum and puts the other information in the right place
have you thought of javascript? it might be faster to use javaascript

Posted: Mon Sep 15, 2003 2:37 pm
by SantaGhost
how are you getting the data? do you input it yourself or does it come from a database? are you looking for listing a directory?