Forum Moderators: coopster

Message Too Old, No Replies

sorting multiple array

         

si3w82

4:31 am on Aug 14, 2006 (gmt 0)

10+ Year Member



hi good day,
can someone help me, my code didnt work.

my code as following:
// to list all the folder and file in the directory
function scanfile($dirName,$doRecursive) {
$d = dir($dirName);
$lastModified = 0;
$directory = array();
$row = 0;
while($entry = $d->read()) {
if ($entry!= "." && $entry!= ".."){
if (!is_dir($dirName."/".$entry) ) {
$directory[$row]['name'] = $entry;
$directory[$row]['link'] = $dirName."/".$entry;
$directory[$row]['last modified date'] = date("d.m.Y h:i",filemtime($dirName."/".$entry));
$directory[$row]['size'] = formatfilesize(get_size($dirName."/".$entry));
$row = $row +1;
}else if ($doRecursive && is_dir($dirName."/".$entry)) {

}else{
$directory[$row]['name'] = $entry;
$directory[$row]['link'] = $dirName."/".$entry;
$directory[$row]['last modified date'] = date("d.m.Y h:i",filemtime($dirName."/".$entry));
$directory[$row]['size'] = formatfilesize(get_size($dirName."/".$entry));
$row = $row +1;

}
}
}
$d->close();

return $directory;
}

$tittle_array = array();
$array = scanfile($path,0);
print_r($array);
$arrayRow = sizeof($array);
for($row = 0; $row<$arrayRow; $row++){
$tittle_array[$row] = $array[$row]['name'];
}
// This will sort on value
//$soft ="sort=1";
if($_GET['sort'] == '1'){
//array_ukmultisort($array, "cmp");
$sorted = BubbleSort($tittle_array,1);
$soft ="sort=0";
}else{
$sorted = BubbleSort($tittle_array,0);
$soft ="sort=1";
}

i need to sort the all file and folder in the directory when i click on the following header:
---------------------------
Name ¦Last modified ¦ size¦
---------------------------
a.file 10/08/2006 10KB

<th colspan="2" class="thH">&nbsp;<a href="?<?php echo $soft?>" >Name</a></th>

pls help me, i dont know how to solve it.
thx

jatar_k

5:34 pm on Aug 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume the issue is that it isn't sorting

where is the code for the actual sorting? I see a couple of user defined functions there but no code.

BubbleSort being one

you might have a typo here

array_ukmultisort

is that user defined or should it be array_multisort? (yes I realize it's commented but I was wondering if that may be part of why some attempts didn't work)

I assume you are trying to sort a multidimensional array like this

$directory[$row]['name'] = $entry;
$directory[$row]['link'] = $dirName."/".$entry;
$directory[$row]['last modified date'] = date("d.m.Y h:i",filemtime($dirName."/".$entry));
$directory[$row]['size'] = formatfilesize(get_size($dirName."/".$entry));

array_multisort [php.net] should work

si3w82

3:50 am on Aug 15, 2006 (gmt 0)

10+ Year Member



hi good day,
thx for ur information..
actually i oledy make change to my code as below:
// to list all the folder and file in the directory

function scanfile($dirName,$doRecursive) {
$d = dir($dirName);
$lastModified = 0;
$directory = array();
$row = 0;
while($entry = $d->read()) {
if ($entry!= "." && $entry!= ".."){
if (!is_dir($dirName."/".$entry) ) {
$directory[$row]['name'] = $entry;
$directory[$row]['link'] = $dirName."/".$entry;
$directory[$row]['last modified date'] = date("d.m.Y h:i",filemtime($dirName."/".$entry));
$directory[$row]['size'] = formatfilesize(get_size($dirName."/".$entry));
$row = $row +1;
}else if ($doRecursive && is_dir($dirName."/".$entry)) {

}else{
$directory[$row]['name'] = $entry;
$directory[$row]['link'] = $dirName."/".$entry;
$directory[$row]['last modified date'] = date("d.m.Y h:i",filemtime($dirName."/".$entry));
$directory[$row]['size'] = formatfilesize(get_size($dirName."/".$entry));
$row = $row +1;

}
}
}
$d->close();

//sort the value
if($_GET['sort'] == '1'){
rsort($directory);
//array_multisort("name", SORT_DESC, SORT_STRING,"last modified date", SORT_NUMERIC, SORT_DESC,
$directory );
print_r($directory);
}else{
sort($directory);
//array_multisort("name", SORT_ASC, SORT_STRING,"last modified date", SORT_NUMERIC, SORT_ASC,
$directory );
print_r($directory);

}

return $directory;
}

$tittle_array = array();
$array = scanfile($path,0);

// This will sort on value
$soft ="sort=1";
if($_GET['sort'] == '1'){
$soft ="sort=0";
scanfile($path,0);
}else{
$soft ="sort=1";
scanfile($path,0);
}

<th colspan="2" class="thH">&nbsp; <a href="?<?php echo $soft;?>">Name</a></th>

the code seem work on 1st record only but when i use array_multisort, the program cannot sort anymore. i dont know wat happen..
pls give my some opinion.

jatar_k

4:58 pm on Aug 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hard to say since your sort lines are commented and I am not sure what you have tried

I would try something simple

array_multisort("name", SORT_DESC, SORT_STRING);

and see what that returns. If it doesn't work then you can try binding/sorting other columns based on what the first attempt returned