Change Display Order from Bottom-Up to Top-Down

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
Smedley
Forum Newbie
Posts: 5
Joined: Tue Feb 26, 2008 10:14 am

Change Display Order from Bottom-Up to Top-Down

Post 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>";
?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

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

Post 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>";
    }
?>
Smedley
Forum Newbie
Posts: 5
Joined: Tue Feb 26, 2008 10:14 am

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

Post by Smedley »

Hmmm...

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

Thanks so much for the help!!! :D
Post Reply