[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ReadDir (php) sorting problem - sort by date required

Posted on 2004-10-10
2
Medium Priority
?
2,595 Views
Last Modified: 2012-05-05
How would I sort the following code so I get it sorted by date. At present it lists files on my web page in Alphabetical order, I want it in date order.

<?php
//Get a cursor toward the folder of interest
$rep = "newsletters/";
$dir = opendir($rep);

// read the folder by selecting the folders (is_dir) or the folders (is_file)
   function dd($date) {
   //return date("m/d/Y H:i:s",$date);
   return date("d F Y",$date);
}
?><TABLE width="100%">
<!--<TR>
        <TD class="textbold" width="5%">&nbsp;</TD>
        <TD class="textbold" width="50%">Name</TD>
        <TD class="textbold" width="20%">Size</TD>
        <TD class="textbold" width="25%">Published</TD>
</TR> -->
<?
while ($f = readdir($dir)) {
   if(is_file($rep.$f)) {

   ?>
           <TR>
           <TD class="text" width="5%"><a href="<? echo "newsletters/".$f?>"><? echo "<img src=\"../media/pdf.gif\" border=0></a>"; ?></TD>
           <TD class="text" width="40%"><a href="<? echo "newsletters/".$f?>"><? echo $f;?></a></TD>
           <TD class="text" width="30%"><? echo filesize($rep.$f)." bytes";?></TD>
           <TD class="text" width="25%"><? echo dd(fileatime($rep.$f));?></TD>
           </TR>
  <?
  }
}
?></TABLE><?

//finally, close the folder
closedir($dir);
?>
0
Comment
Question by:stephenreed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
sajuks earned 375 total points
ID: 12273713
//adjust according to your needs
<?
$files = array();
$files_directory = "c:/path";
$fdirectory = opendir("$files_directory");
    while (false!==($file=readdir($fdirectory))) {
        if (($file != ".") && ($file != ".." )) {
        $files[$file] = filemtime("$files_directory/$file");
        }
    }
closedir($fdirectory);
clearstatcache();
arsort($files);
echo "<table>";
while(list($key, $val)=each($files))
    echo "<tr><td>$key</td><td>".date("M j, h:ia", $val)."</td></tr>\n";
echo "</table>";
?>
0
 

Author Comment

by:stephenreed
ID: 12281941
Used the above code but did not use arsort($files);
 but used the asort($files); function in its place and sorted by chronological order (date order). The explanation (code) was quite minimal and would require some programing ability so I would not give this answer full marks as it needed some instructions to modify for different sort orders.
Stephen
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question