0

I want to include a lot of .php files. Reason beeing that i have 4 categories of work that need be showcased.

The following code works. Though, it is basically everything i know about PHP. I am not firm with writing PHP.

<?php
if(!isset($_GET['page'])) $_GET['page']="";
switch($_GET['page']) {

case "category_a": 
    include('site1.php');
    include('site2.php');
    include('site3.php');
    include('site4.php'); 
break;
case "category_b": 
    include('site5.php');
    include('site6.php');
    include('site7.php');
    include('site8.php');
    include('site9.php');
    include('site10.php'); 
break;

}
?>

Since there are so many included sites, and i want to be able to include those categories elsewhere without listing every file again (and therefore updating the list on multiple sites).

Is it possible to simply include a path? :D This is how i imagine the solution:

case "category_a": 
    include('/directory/category_a'); 
break;

Any help is very very appreciated.

Antony
  • 14,900
  • 10
  • 46
  • 74
  • 2
    already asked and answered: http://stackoverflow.com/questions/599670/how-to-include-all-php-files-from-a-directory – Matanya Jul 14 '13 at 13:26
  • Hi Matanya. I appreciate your help. But, i read this question multiple times before posting my question. I simply dont understand their solutions. What are classes? They dont have cases. It is, sadly, beyond my understanding. – Typetecture Jul 14 '13 at 13:42
  • Ehem, i probably managed to solve my problem by try and error copy pasting the highest voted code from your linked question in random places. It seems to work. Much love. Please excuse my royal dumbness. – Typetecture Jul 14 '13 at 13:54
  • Please put [your answer](http://stackoverflow.com/revisions/17639783/2) in the answer section below. – Antony Jul 14 '13 at 14:50
  • Thanks for your patience. I will do so when i see fit. – Typetecture Jul 14 '13 at 14:57

1 Answers1

0
function includeFilesFromDir ( $dir_path, $file_extension, $recursive = false ){
    if(!is_dir( $dir_path )){
        return false;
    }
    if(empty( $file_extension )){
        return false;
    }
    $all_dir_files = scandir( $dir_path );
    foreach($all_dir_files as $file){
        if( $file == "." || $file == ".." ){
            continue;
        }
        if( is_dir( $dir_path . "/" . $file ) && $recursive ){
            includeFilesFromDir( $dir_path . "/" . $file, $file_extension, $recursive);
        } elseif( $file_extension == pathinfo( $file, PATHINFO_EXTENSION ) ){
            include( $dir_path . "/" . $file );
        }

    }

    return true;
}
includeFilesFromDir("C:/some_dir", "php", true);
liding
  • 116
  • 1
  • 6
  • Hi liding. Your solution works too. I dont want to steal anymore time from you. Just incase you have a spare minute: What advantages does your very elaborate solution bring to the table compared to the "simpler" code i posted above as a solution? In any case, many thanks. – Typetecture Jul 14 '13 at 14:24
  • 1
    It adds better maintenance! You don't need to do the include-step after adding an include file to the "includes"-directory in the future. So you can't forget to include something. – Dennis Ziolkowski Jul 14 '13 at 15:30