views:

50

answers:

1

Hello,

I have this code which will include "template.php" file from inside each of these folders: "content/templates/id1", "content/templates/id2", "content/templates/id3" etc. etc.

$page_file = basename(__FILE__, ".php");
require("content/" . $page_file . "/content.php");
$iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($page_path), 
            RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $file) {
    if($file->isDir()) {
        include strtoupper($file . '/template.php');
    }
}

This code works pretty well, the problem is I want to inverse the content adding, meaning that I want first "content/templates/id9/template.php" included before "id8/template.php" and so on till the first..

How can I do this by modifying the code above? A million thanks!

+2  A: 

Instead of including the files immediately, add them to an array and call array_reverse

$page_file = basename(__FILE__, ".php");
require("content/" . $page_file . "/content.php");

$includes = array();

$iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($page_path), 
            RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $file) {
    if($file->isDir()) {
        $includes[] = strtoupper($file . '/template.php');
    }
}

$includes = array_reverse($includes);

foreach($includes as $file){
   include $file;
}
Josh
You sir are a Genius! :DThanks a lot!
Adrian M.