Page 1 of 1

A question about reverse sorting

Posted: Wed Apr 04, 2007 9:53 am
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.

Posted: Wed Apr 04, 2007 9:58 am
by feyd
Store the list into an associative array where the key is in a form PHP can automatically sort (e.g. YYMMDD blah)

Posted: Wed Apr 04, 2007 10:07 am
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.

Posted: Wed Apr 04, 2007 10:08 am
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.

Posted: Wed Apr 04, 2007 10:28 am
by onion2k
I've just posted something that will do exactly what you want somewhere else on here... viewtopic.php?p=371231#371231

Posted: Wed Apr 04, 2007 12:11 pm
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: