The 10 most recent updates on a website

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
chapyl
Forum Newbie
Posts: 5
Joined: Sat Jun 25, 2005 11:31 am

The 10 most recent updates on a website

Post 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>";
}
?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

try this?

Post 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.
chapyl
Forum Newbie
Posts: 5
Joined: Sat Jun 25, 2005 11:31 am

Post 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 )
chapyl
Forum Newbie
Posts: 5
Joined: Sat Jun 25, 2005 11:31 am

Post 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. :?
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post 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...
chapyl
Forum Newbie
Posts: 5
Joined: Sat Jun 25, 2005 11:31 am

Post 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.
chapyl
Forum Newbie
Posts: 5
Joined: Sat Jun 25, 2005 11:31 am

Post by chapyl »

Now it works!!! YES!!!
Thanks everyone!
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

Can you post the fixed code?
Post Reply