I'm a bit of a n00b when it comes to PHP, anyway, I am in process of making a website about Gaming News, and I decided to put all of my different news stories into different .txt files on a different directory. I have everything done fine except one thing: my news pieces are shown in the alphabetical order of the .txt filenames. That means that the latest story could be on page 53 or whatever. I have tried making codes that shows the news in the order of the creation/modification date of the .txt file.
Here is what I tried:
PHP Code:
<?php
function returnfiles($dirname="docs/") {
$pattern="\.(txt)$";
$files = array();
$fileloopcounter=0;
if($handle = opendir($dirname))
{
while(false !== ($file = readdir($handle)))
{
if(eregi($pattern, $file)){
$filedate=filemtime($dirname.$file);
$files[] = array($file, $filedate);
$fileloopcounter++;
}
}
closedir($handle);
}
return($files);
}
function DateCmp($a, $b)
{
return ($a[1] < $b[1]) ? -1 : 0;
}
function SortByDate($files)
{
usort($files, 'DateCmp');
}
$files = returnfiles($dirname="docs/");
SortByDate($files);
echo '<ol>';
foreach ($files as $file => $val)
{
echo '<li>', $val, '</li>';
}
echo '</ol>';
?>
And then I just get this when I open the file on a browser:

Can someone please help me with my problem?
Thanks in advance
.
Bookmarks