Modify this function to display layouts by Date Added

Discussion in 'PHP' started by JosS, Sep 26, 2007.

  1. #1
    I have a function like so:
    
    if ($handle = opendir('layouts')) {
       while (false !== ($file = readdir($handle))) {
           if ($file != "." && $file != "css" && $file != "..") {
               $files[] = $file;
           }
       }
       closedir($handle);
    }
    
    $array_lowercase = array_map('strtolower', $files);
    array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $files);
    
    PHP:
    and then I use a for loop to echo each of the files

    for($e=$perpage*($page-1);$e<$last;$e++){
    PHP:

    How could I get it to display the latest 20 files added by file date? At the moment its in alphabetical order

    Cheers
     
    JosS, Sep 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try this.
    
    $filetimes = array_map('filectime', $files);
    array_multisort($filetimes, SORT_NUMERIC, SORT_DESC, $files);
    unset($filetimes);
    
    PHP:
    $files should not be sorted by file creation time, in descent order.
     
    nico_swd, Sep 26, 2007 IP
  3. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm getting a huge error listing for all the files like so:

    Warning: filectime() [function.filectime]: Stat failed for image-of-tigers.jpg (errno=2 - No such file or directory) in
     
    JosS, Sep 26, 2007 IP
  4. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is what I got now

    
    <?php
    if ($handle = opendir('layouts')) {
       while (false !== ($file = readdir($handle))) {
           if ($file != "." && $file != "css" && $file != "..") {
               $files[] = $file;
           }
       }
       closedir($handle);
    }
    
    $array_lowercase = array_map('strtolower', $files);
    $filetimes = array_map('filectime', $files);
    array_multisort($filetimes, SORT_NUMERIC, SORT_DESC, $files);
    unset($filetimes);
    
    PHP:
    it prints out lines of that error, and the displays them as they normally would without the added code
     
    JosS, Sep 26, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    My bad, I forgot you didn't include the directory names in the $files array. This does work. (Tested)
    
    if ($handle = opendir('layouts')) {
       while (false !== ($file = readdir($handle))) {
           if ($file != "." && $file != "css" && $file != "..") {
               $files[] = "layouts/{$file}";
           }
       }
       closedir($handle);
    }
    
    $filetimes = array_map('filectime', $files);
    array_multisort($filetimes, SORT_NUMERIC, SORT_DESC, $files);
    unset($filetimes);
    
    // Get rid of directory names
    $files = array_map('basename', $files);
    
    print_r($files);
    
    
    PHP:
     
    nico_swd, Sep 26, 2007 IP
  6. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Works a treat, I figured thats what was going on but didn't know how to implement it inject layouts/ before each value in the array.

    Cheers nico, you've come to the rescue. Yet again :D
     
    JosS, Sep 26, 2007 IP