Forum Moderators: coopster

Message Too Old, No Replies

mtime problem with gallery script

reverse mtime

         

Champak

5:38 pm on Aug 4, 2006 (gmt 0)

10+ Year Member



Please help with the following code. The creator no longer supports it, I don't want to change scripts, and I'm trying to do something simple to it. It is set up to order the pictures by the date they were modified----"mtime", I would like to reverse the mtime so it is ordered latest to oldest, instead of oldest to latest. Any help please. I have edited out a good portion of this script that I THINK is not needed in order to figure this out because of its size and being unable to post, if the rest is needed please let me know.

<snipped code, see next post>

[edited by: coopster at 8:32 pm (utc) on Aug. 4, 2006]
[edit reason]
[1][edit reason] kept relevant code [/edit]
[/edit][/1]

coopster

8:31 pm on Aug 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It all happens when the directory is read. You just need to modify that function, maybe pass a sort sequence to it and set a default so that you won't have to modify any of the existing processes except where you are calling the function and you want it loaded in a different sort order. I see that the original code even has a sort command commented out. Note the bold changes and click the linked functions for more information:
function [php.net] load_dir($sortBy = 'name', $sortOrder = 'ASC') 
{
global $dirs;
global $imgs;
global $pref;
$lastmod = 0;
$dh = opendir('.');
while (($file = readdir($dh)) !== false) {
if (is_dir($file)) {
$dirs[] = $file;
} else if (is_readable($file)) {
$ext = getValidExtension($file);
if($ext) {
$when = filemtime($file);
$imgs[$file] = $when;
if ($when > $lastmod) {
$lastmod = $when;
}
//} # end if ft
}# end elseif
} #end while
closedir($dh);
if ($sortBy == 'name') {
ksort [php.net]($imgs);
} else {
asort [php.net]($imgs);
}
if ($sortOrder == 'DESC') {
$imgs = array_reverse [php.net]($imgs, true);
}

//print "Sort By: $sortBy\nSort Order: $sortOrder\n";
//print '<pre>'; print_r($imgs); print '</pre>';
return $lastmod;
#sort($imgs);
} #end load_dir

Test run the code:

// A default listing (name, ascending): 
load_dir();
//load_dir('name', 'ASC'); // <- Same as above
// Sort by name, descending
load_dir('name', 'DESC');
// Sort by time, ascending
load_dir('time');
//load_dir('time', 'ASC'); // <- Same as above
// Sort by time, descending
load_dir('time', 'DESC');