Page 1 of 1

The 10 most recent updates on a website

Posted: Sat Jun 25, 2005 12:34 pm
by chapyl
First post in this forum, I'm rather new to Php but find it interesting. I also hope you understand my English since it's not my language.


I have a website and would like to list the ten recently updated pages in a table with 'title' of the page and date changed and with a link to that page.

Like:

Links ( 2005-06-19 22:57 )

-------------

A guy on another forum suggested a code... and it works pretty well
except that the order isn't according to the date when the pages is updated. Instead I get a list of the most recent updated pages according to alphabetical order. I guess something is wrong, probably the 'rsort' command because it list according to filenames, but I have tried to change it but to no success.
The guy who helped me with the script knows PHP much better than me but couldn't see what was wrong in the code.

As I said I'm not good in PHP but maybe someone with better knowledge in PHP understands what's missing in the code?


Code: Select all

<?php

$dir = "./";

// searching of filetypes
$filetypes = Array( "php","htm" );

// the title
// [pagename] => [name]
$titles = Array( "index.php"=>"startsidan", "heja.php"=>"hejsidan" );

$foundtype = "";

if ( is_dir($dir) ) {
if ( $dh = opendir($dir) ) {

$filenames = Array();

while ( ($file = readdir($dh) ) != false) {
$foundtype = explode(".",$file);
if ( ($file != "." && $file != ".." && $file[0] != '.') && array_key_exists($file, $titles)) {
$filenames[] = $file;
}
}
closedir($dh);
}
}

rsort($filenames);

$maxnum = 10;
$count = count( $filenames );
if ( $count > 10 )
$count = $maxnum;

if ( $count > 0 ) {
echo "$count recent updated pages:<p>";
for ($i = 0; $i < $count; $i++) {
// namn som visas om inte filen finns med i listan
$tmp = "Untitled";
if ( array_key_exists($filenames[$i], $titles) ) {
$tmp = $titles[$filenames[$i]];
}
echo ($i+1).". <a href=\"$filenames[$i]\">".$tmp."</a> ( ".date("Y-m-d H:i:s", filemtime($filenames[$i]))." )<br>";
}
echo "</p>";
}
else {
echo "<p>Didn't found any webpages.</p>";
}
?>

try this?

Posted: Sat Jun 25, 2005 3:29 pm
by neophyte
You have to use filectime() to find out when the file was created. Try inserting this into your code and see if you get the results you are looking for:

Code: Select all

while ( ($file = readdir($dh) ) != false) {
$foundtype = explode(".",$file);
if ( ($file != "." && $file != ".." && $file[0] != '.') && array_key_exists($file, $titles)) {
$filenames[filectime($file)] = $file;
}
}
closedir($dh);
}
}
 
ksort($filenames);
I haven't tested, but I think it'll work. Should return an array with the time created/updated as key and the file name as the value.

Posted: Sat Jun 25, 2005 4:00 pm
by chapyl
Thanks for your help, but unfortunately it didn't work. :?


Before I got this result:
1. Personal ( 2005-06-25 22:49:47 )
2. Links ( 2005-06-25 22:50:47 )


Now I got this result:
1. Untitled ( 1970-01-01 01:00:00 )
2. Untitled ( 1970-01-01 01:00:00 )

Posted: Sun Jun 26, 2005 2:53 pm
by chapyl
The strange thing with the original script is that it worked for the guy who wrote it, no problems with the update order. The file most recently updated was listed first - no alphabetical order. :?

Posted: Tue Jun 28, 2005 12:05 pm
by phpnewbdude
I would like to use this script as well, but is there a way to modify it where it would look at all file extensions that end with .htm instead of defining them in an array?

I'm new to php - could someone modify the code above?

The date thing really doesn't bother me... i would just remove the date stamp...

Posted: Tue Jun 28, 2005 1:13 pm
by chapyl
phpnewbdude:
The date thing really doesn't bother me... i would just remove the date stamp...


Maybe I will have to change it too since it seems hard to make it work. :cry:
Hope someone can make it work.

Posted: Mon Jul 11, 2005 11:51 am
by chapyl
Now it works!!! YES!!!
Thanks everyone!

Posted: Mon Jul 11, 2005 11:53 am
by phpnewbdude
Can you post the fixed code?