A question about reverse sorting

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
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

A question about reverse sorting

Post by jleampark »

I have a php page which looks in a documents folder and pulls all the names and hyperlinks them. The names of the douments start with a date (for example, "01_09_07 Document 1" and "03_06_07 Document 2")

Currently, there is no real sort. The file system automatically sorts them lower numbers to higher.

My clients wants them sorted in descending order so that the most recent documents appear before the older documents.

I am sure it's easy to do; alas, I am a noob.

Here is my code:

Code: Select all

<?php 
                        $dir = "../esmpd_minutes";
                        if ($handle = opendir($dir)) {
                          while (false !== ($file = readdir($handle))) { 
                            if ($file != "." && $file != "..")  { 
                              echo "<li class='.listitem'><a href=\"" . $dir . "/" . $file . "\">" . $file . "</a></li><br />\n"; 
                            } 
                          }
                          closedir($handle);
                        }
                        ?>
Is there something easy to change or add?

Dumb it down for me due to my afor-mentioned noob-ness.

Please and thank you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Store the list into an associative array where the key is in a form PHP can automatically sort (e.g. YYMMDD blah)
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

Post by jleampark »

Well, we perdiodically drop new documents into this folder so I don't think a static array will work. I need this page to search the folder each time to see what documents are currently present and then dynamically sort them, in descending order.

Any ideas?

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I didn't say anything about a static array. You perform your directory query as per normal, storing the files into an array.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I've just posted something that will do exactly what you want somewhere else on here... viewtopic.php?p=371231#371231
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

Post by jleampark »

Onion,

That is a beautiful piece of code.

Pat yourself on the back and give yourself the rest of the day off. And if anyone questions you, tell them to take it up with me.

:wink:
Post Reply