Page 1 of 1

Change Display Order from Bottom-Up to Top-Down

Posted: Mon Mar 15, 2010 12:54 pm
by Smedley
Hello,

I have a web page that sorts the contents (PDFs) of a directory and displays them on the webpage chronologically from the oldest (at top) to the newest (at bottom).

I'd like to reverse this display order and have the latest files display at the top with oldest at the bottom. I haven't seen any info on how to accomplish this yet and hope someone can help. The code is seen below:

Thanks in advance for any help or suggestions!!! :D

<?php
$dir = opendir('../../files/main/group/info');
$i = 0;
// begin subdirectory processing
$subdir = "../../files/main/group/info"."/";
$cwd = opendir($subdir);
echo '<ul style="list-style: none;"><strong>';
while ($file = readdir($cwd)) {
if (($file != ".") && ($file != "..") && ($file != "README.txt")) {
$link = rawurlencode($file);
$tmp = basename($file, ".pdf");
$filename = date('F d, Y',strtotime($tmp));
echo '<li><img src="../../images/icons/pdf.gif" alt="PDF Document" />&nbsp;<a href="' . $subdir, $link . '" target="_blank">' . $filename . '</a>';
}
}
echo "</strong></ul>";
?>

Re: Change Display Order from Bottom-Up to Top-Down

Posted: Mon Mar 15, 2010 1:24 pm
by flying_circus
This is untested, so you might need to debug it. Consider it psuedo-code

Code: Select all

<?php
  # Variables
    $links = array();
    $path = '../../files/main/group/info/';
    $cwd = opendir($path);
    
  # Begin directory processing
    while ($file = readdir($cwd)) {
      if (($file != ".") && ($file != "..") && ($file != "README.txt")) {
        $link = array('link' => rawurlencode($file),
                      'tmp' => basename($file, ".pdf"),
                      'filename' => date('F d, Y',strtotime($tmp)));
                      
        $links[] = $link;
      }
    }
  
  # Reverse Sort Array
    krsort($links);
    
  # Output Links
    if($count($links)) {
      echo '<ul style="list-style: none;"><strong>';
      
      foreach($links as $link) {
        echo '<li><img src="../../images/icons/pdf.gif" alt="PDF Document" />&nbsp;<a href="' . $path, $link['link'] . '" target="_blank">' . $link['filename'] . '</a></li>';
      }
      
      echo "</strong></ul>";
    }
?>

Re: Change Display Order from Bottom-Up to Top-Down

Posted: Wed Mar 17, 2010 8:44 am
by Smedley
Hmmm...

I'll give that a try and see how it does.

Thanks so much for the help!!! :D