include files or function in a for loop possible?

Discussion in 'PHP' started by gilgalbiblewheel, Sep 25, 2008.

  1. #1
    I'm trying to simplify my page by putting the include files in a for loop. What's the solution?
    function includeNames1(){
    include("getFiles/links.php");
    }
    function includeNames2(){
    include("getFiles/blog.php");
    }
    function includeNames3(){
    include("getFiles/search1.php");
    }
    function includeNames4(){
    include("getFiles/search2.php");
    }
    function includeNames5(){
    include("getFiles/search3.php");
    }
    function includeNames6(){
    include("getFiles/search4.php");
    }
    function includeNames7(){
    include("getFiles/search5.php");
    }
    function includeNames8(){
    include("getFiles/home.php");
    }
    function includeNames9(){
    include("getFiles/blank.php");
    }
    PHP:

     
    gilgalbiblewheel, Sep 25, 2008 IP
  2. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to include() all of the .php files in a specific directory and you don't care what order they are in maybe you could use something like this?:

    <?php
    $includesDir = '/projectdirectory/includes';
    if ($handle = opendir($includesDir)) {
            while ($file = readdir($handle)) {
                    if (strtolower((strlen($file) >3)) && (substr($file,-4) == '.php')) {
                            include($includesDir . "/" . $file);
                    }
            }
            closedir($handle);
    }
    ?>
    PHP:
     
    drunnells, Sep 25, 2008 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Well it's in the order of the array $includeNames:

    $includeNames = Array("links", "blog", "search1", "search2", "search3", "search4", "search5", "home", "blank");
    $theNames = Array("Navigation", "Blog", "Look For", "Additional Textboxes", "Choose a Spoke:", "Other selections:", "Select From Books:", "", "");
    $divisionId = Array("links", "my_blog", "search_1", "search_2", "search_3", "search_4", "search_5", "welcome_page", "results_page");
    
    for($i=0; $i<count($divisionId); $i++){	
    	if($i<7){
    		echo "				<div id=\"".$divisionId[$i]."\" style=\"float: left; margin-top: 10px;\">\n";
    		echo "					<div style=\"float: left; width: 100%; margin: 0px; background-color: #7C7C7C; border: 1px solid #A5A498;\">\n";
    		echo "						<div style=\"padding: 2px 0px 0px 10px; height: 20px; color:#FFFFFF; font-family: arial; font-weight: bold; font-size: 13px;\">\n";
    		echo "							".$theNames[$i]."\n";
    		echo "							<span id=\"span_minmaxbox_".$i."\" style=\"float: right; padding-left: 5px; width: 20px; height: 15px; background-color: #000000; font-size:10px;\">\n";
    		echo "								<a id=\"minmaxbox_".$i."\" href=\"#\" style=\"text-decoration: none; color: #FFFFFF;\" onmouseover= \"this.style.color='red';\" onmouseout=\"this.style.color='white';\" onclick=\"openBox();\">[ + ]\n";
    		echo "								</a>\n";
    		echo "							</span>\n";
    		echo "						</div>\n";
    		echo "					</div>\n";
    		
    		$includeFile = "getFiles/".$includeNames[$i].".php";
    		//echo $includeFile."\n";
    		include($includeFile);
    		//include("getFiles/".$includeNames[$i].".php");
    		echo "				</div>\n";
    		//echo "			</div>\n";
    	}
    	
    	if($i==7){
    		echo "				<div id =\"".$divisionId[$i]."\" style=\"float: right; margin: 0px 10px 0px 0px; width: 645px; height: 100%; padding: 10px 10px 0px 10px; background-color:#D2C5A0; border:1px solid #B7B7B7; z-index: 3; visibility: visible;\">\n";
    include("getFiles/".$includeNames[$i].".php");
    echo "".$theNames[$i]."\n				</div>\n";
    	}
    	if($i==8){
    		echo "				<div id =\"".$divisionId[$i]."\" style=\"float: right; margin: 0px 10px 0px 0px; width: 645px; height: 580px; padding: 15px 10px 10px 10px; background-color:#D2C5A0; border:1px solid #B7B7B7; z-index: 1; overflow-y: auto; overflow-x: hidden; visibility: visible;\">\n";
    	include("getFiles/".$includeNames[$i].".php");
    	echo "".$theNames[$i]."\n				</div>\n";
    	}
    	
    
    }
    PHP:
    I don't understand why but:
    $includeNames = Array("links", "blog", "search1", "search2", "search3", "search4", "search5", "home", "blank");
    PHP:
    after "links" the next one it remembers is "search5" and that's it.
     
    gilgalbiblewheel, Sep 25, 2008 IP