Page 1 of 1

GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 3:22 pm
by arbitter
Well, the problem is simple. I have a directory uploads/ with directory of the months in it. For now, it's only "January '10", but on the first of February it's likely the folder "Fabruary '10" will be added.

I have:

Code: Select all

foreach (glob('*',GLOB_ONLYDIR|GLOB_NOSORT) as $map)
{echo $map;}
The file containing this is also in my directory uploads/ so all the directories of the months get shown. But unfortunately, they get shown in the wrong order. If I add a directory February '10, it goes onn top of January '10. But I want it to be the other way around; the oldest directory on top. How will I be able to fullfill my wish?

Re: GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 4:03 pm
by manohoo
You want to sort by creation date, which is not part of the filename.

I suggest that you change your directory names to something like this:
January '10 to 201001
February '10 to 201002
...

That way you can easily sort the files.

Re: GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 4:13 pm
by Eran
Iterate over the array and use filemtime() to sort them manually.
http://php.net/manual/en/function.filemtime.php

Re: GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 4:18 pm
by arbitter
But how do I incorporate that in my foreach(glob()) ?

Re: GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 4:22 pm
by Eran
are you a programmer?

Re: GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 4:28 pm
by arbitter
beginner, working on my first site so not really...

Re: GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 4:35 pm
by Eran
Basically you have to iterare the glob results twice. once to sort it, and another to display the content. glob() returns an array of directories -

Code: Select all

$dirs = glob('*',GLOB_ONLYDIR|GLOB_NOSORT);
You iterate over it once to sort out the contents:

Code: Select all

foreach($dirs as $dir) {
    $sortedDirs[filemtime($dir)] = $dir;
}
ksort($sortedDirs);
Use krsort (reverse key sort) instead of ksort if you want the sorting to be descending by modification date (instead of ascending). You can then iterate over it as you did before to display the contents.

Re: GLOB_NOSORT; need to show files in order of date changed

Posted: Fri Jan 08, 2010 4:59 pm
by arbitter
After some trying and cursing I get:

Code: Select all

$dirs = glob('*',GLOB_ONLYDIR|GLOB_NOSORT);
            foreach($dirs as $dir) {
                $sortedDirs[filemtime($dir)] = $dir;
                }
            ksort($sortedDirs);
            foreach (($sortedDirs) as $map)
                {   $qmap = urlencode($map);
                    echo "<tr><td bgcolor='white' width='200' align='center' valign='center' style='cursor:pointer;cursor:hand' onclick=\"location.href='?month=$qmap'\"><center>$map</td></tr>";
                }
which seems to work perfectly!
Thank you very much!