GLOB_NOSORT; need to show files in order of date changed

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
arbitter
Forum Newbie
Posts: 24
Joined: Tue Dec 29, 2009 10:04 am

GLOB_NOSORT; need to show files in order of date changed

Post 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?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

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

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

Iterate over the array and use filemtime() to sort them manually.
http://php.net/manual/en/function.filemtime.php
arbitter
Forum Newbie
Posts: 24
Joined: Tue Dec 29, 2009 10:04 am

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

Post by arbitter »

But how do I incorporate that in my foreach(glob()) ?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

are you a programmer?
arbitter
Forum Newbie
Posts: 24
Joined: Tue Dec 29, 2009 10:04 am

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

Post by arbitter »

beginner, working on my first site so not really...
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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.
arbitter
Forum Newbie
Posts: 24
Joined: Tue Dec 29, 2009 10:04 am

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

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