Sortable Table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nick Ewing
Forum Newbie
Posts: 3
Joined: Sun Sep 14, 2003 4:02 pm

Sortable Table

Post 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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

try looking at array_sort()
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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?
Nick Ewing
Forum Newbie
Posts: 3
Joined: Sun Sep 14, 2003 4:02 pm

the table

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Nick Ewing
Forum Newbie
Posts: 3
Joined: Sun Sep 14, 2003 4:02 pm

Post by Nick Ewing »

thanks but im rather new to this and am having problems working with that.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Re: the table

Post 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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
SantaGhost
Forum Commoner
Posts: 41
Joined: Mon Sep 15, 2003 11:54 am

Post 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?
Post Reply