File ordering by date modified

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
semas
Forum Newbie
Posts: 10
Joined: Sat Oct 25, 2008 11:00 am

File ordering by date modified

Post by semas »

Hello everyone, i need some help with file ordering, i need to order files by date modified.
Here is my code:

Code: Select all

 
<?php
$handle = opendir('foto/');
    while ($file = readdir($handle)) {
        $files[] = $file;
    }
    closedir($dir_handle);
    
    sort($files);
    foreach ($files as $k => $file) {
        if($file != "." && $file != ".." && $file != "index.php" && $file != "style.css" && $file != "index2.php" && $file != "0.jpg"){
            $id++;
            if($id_c == $id){
                $s_file = $file;
                $id_cc++;
            }
        }
    }
?>
If someone will help me i'll be realy thankfull.
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: File ordering by date modified

Post by Hannes2k »

Hi,
just use usort():

There are two ways:
Create a new array (like $files) but don't just save the filename into it. Create for each entry in $files a new array with 2 elements, first element is the filetime (filectime()), second the filename ($file).

Now you can sort the array with usort():

Code: Select all

 
$files = array();
 
//open dir etc.
while(...) {
  $files[] = array(filectime($file), $file);
 
 
//Compares the filectime of to two files
function fileCompare($a, $b) {
    return $a[0] - $b[0];
}
 
usort($files, "fileCompare");
 
//print $files
 

The other would be to call filectime() in the fileCompare function:

Code: Select all

 
$files = array();
 
//Your old while loop
while(...) {
  $files[] = $file;
 
 
//Compares the filectime of to two files
function fileCompare($a, $b) {
    return filectime($a) - filectime($b);
}
 
usort($files, "fileCompare");
 
//print $files
 
Last edited by Hannes2k on Sat Oct 25, 2008 3:16 pm, edited 1 time in total.
semas
Forum Newbie
Posts: 10
Joined: Sat Oct 25, 2008 11:00 am

Re: File ordering by date modified

Post by semas »

Ok, now im confused. Site made for funny pictures (upload allowed for all with admin confirmation). I tried to aply your exaple, but it ordered everything from about center of all photos.

P.S. If this will not work ill make it with MySQL database.
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: File ordering by date modified

Post by Hannes2k »

Hi,
just try this:

Code: Select all

<?php
$handle = opendir('foto/');
    while ($file = readdir($handle)) {
        $files[] = $file;
    }
    closedir($dir_handle);
 
//Compares the filectime of two files
function fileCompare($a, $b) {
    return filectime($a) - filectime($b);
}
 
usort($files, "fileCompare");
 
    foreach ($files as $k => $file) {
        if($file != "." && $file != ".." && $file != "index.php" && $file != "style.css" && $file != "index2.php" && $file != "0.jpg"){
            $id++;
            if($id_c == $id){
                $s_file = $file;
                $id_cc++;
            }
        }
    }
?>
In the first example there had been a small mistake, the index of $a and $b in the compare function have to be 0 not 1.
But this code should worke fine (but I didn't tested it)
semas
Forum Newbie
Posts: 10
Joined: Sat Oct 25, 2008 11:00 am

Re: File ordering by date modified

Post by semas »

Ok, thank you for help, but i made it MySql based.
semas
Forum Newbie
Posts: 10
Joined: Sat Oct 25, 2008 11:00 am

Re: File ordering by date modified

Post by semas »

One more thing. I'm making site chat. Need help with Links that people will post, they use " Link name ".
I used str_replace for another tags, but i think i need preg_match or preg_replace now...
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: File ordering by date modified

Post by Hannes2k »

Hi,
just use Google and search for: PHP + bb codes.

You use preg_replace for this purpose.
Post Reply