0

I am using

foreach(glob("config/pages/*.php") as $page){

to get the list of all files in the directory of config/pages/ Can I have this show up by the oldest file first and the newest file last?

I am wanting to make a navigational menu out of all of this. My full source code is as follows but I need the oldest file to show up first, Maybe I could add a variable to each file like: `$order = 1;' where the number would be the order in the list?

so for example for page home.php the $order would be equal to 0 and about.php would be equal to 1. And then somehow sorting based on counting in order from 0?

<ul class="sidebar-nav">

<li class="sidebar-brand"><a href="#"><?php echo $config['site_title']; ?></a></li>

<?php

    $files = glob("config/pages/*.php");

    $files = array_combine(array_map("filemtime", $files), $files);

    ksort($files);


    foreach($files as $page){

        // Remove the conifg/pages/ from the string
        $strip1 = str_replace('config/pages/', '', $page);

        // Remove the .php from the string
        $strip2 = str_replace('.php', '', $strip1);

        // Uppercase the first letter in the string
        $capit = ucfirst($strip2);

        // Re-define the string as the display title
        $title = $capit;

        // Remove the .php and replace it with .html
        $html = str_replace('.php', '.html', $strip1);

        // Re-define the string for the link url
        $link = $html;

        // Display the end string with html elements to user
        echo "<li><a href='".$link."'>".$title."</a></li>";
    }

?>

Mitch Evans
  • 641
  • 3
  • 10
  • 25
  • On many platforms/filesystems there is no way to tell when a file was "created". The best you can do is last modification date. Will modification date work/Are you on a platform that actually supports creation dates? – kojiro Jan 05 '14 at 04:43

2 Answers2

8

try this code. I used in my project.Please check.

$myarray = glob("config/pages/*.php");
usort($myarray, function($a,$b){
  return filemtime($a) - filemtime($b);
});
nice ass
  • 16,471
  • 7
  • 50
  • 89
Kumar V
  • 8,810
  • 9
  • 39
  • 58
  • This worked! I had actually just found this here: http://stackoverflow.com/questions/124958/glob-sort-by-date so I guess my question was a duplicate :( Whoops. Thought I had searched and tried. – Mitch Evans Jan 05 '14 at 04:44
  • @MitchEvans. Ok fine. Recently I searched and added in my project. So i posted from my project. Ultimate source is Always SO only :-) – Kumar V Jan 05 '14 at 04:45
  • just a note: `filemtime` returns the file's last modification date – nice ass Jan 05 '14 at 04:45
  • @onetrickpony -- Yeah I now see that, When I go and edit a page it changes the order. Won't work. I'll add to my question to describe what I'm wanting to do. – Mitch Evans Jan 05 '14 at 04:48
  • if you want to sort by created date, then you can refer this SO answer: http://stackoverflow.com/a/8168895/270037 – Kumar V Jan 05 '14 at 05:02
  • Argh, no. ctime is the *change time of the inode metadata*. It is not creation time. – kojiro Jan 05 '14 at 05:08
0

I'd do it in three steps. Read your list first. Then make an associative array with file modification times, then sort in ascending order.

$files = glob("config/pages/*.php");
$files = array_combine(array_map("filemtime", $files), $files);
ksort($files);

Thus the oldest files will be atop the list.

mario
  • 144,265
  • 20
  • 237
  • 291