【问题标题】:PHP include file - overriding itself in functionPHP包含文件 - 在函数中覆盖自身
【发布时间】:2016-04-02 13:19:29
【问题描述】:

这是我的代码:

<?php


     if(isset($_POST['load_more'])){
	
     	$variable = $_SESSION['variable']++;
     	generateNewPage($detail_locator, $variable);
     	
    }
    
    function generateNewPage($detail_locator, $variable){
    

    	        $relative_path = "load_more/newfile $variable .php";
		$myfile = fopen($relative_path, "a") or die("Unable to open file!");
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		fclose($myfile);
		
		include "load_more/newfile $variable .php";
		
    
    }
    

?>
<div id="reload_section">
</ul>
<center><br />
	<form  method="post">
		<input type="submit" value="Load More" name="load_more" class="load_more_content" />
	</form>
</center>
</div>

基本上,每次用户单击按钮时,都会通过会话生成一个新页面,每次将 fopen 加 1。

当用户单击按钮时,会生成一个页面,然后包含该页面。

但是,每次运行该函数时,include 语句都会覆盖自身,并且仅包含最新生成的页面。

我希望在用户单击按钮时逐步包含每个生成的页面。

这可能吗?如果是,那怎么办?

@David Jones 的更新:

<?php

	if (!isset($_SESSION['files'])) {
    	$_SESSION['files'] = []; // or array() if your PHP version is < 5.4
	}

	if(isset($_POST['load_more'])){
	
     		$variable = $_SESSION['variable']++;
     		generateNewPage($detail_locator, $variable);
     	
    }
    
    function generateNewPage($detail_locator, $variable){
    

    	$relative_path = "load_more/newfile $variable .php";
		$myfile = fopen($relative_path, "a") or die("Unable to open file!");
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		fclose($myfile);
		
		$SESSION['files'][] = "load_more/newfile $variable .php";
		
		foreach ($_SESSION['files'] as $file) {
    		include $file;
		}
		
    
    }
    

?>
<div id="reload_section">
</ul>
<center><br />
	<form  method="post">
		<input type="submit" value="Load More" name="load_more" class="load_more_content" />
	</form>
</center>
</div>

【问题讨论】:

    标签: php include include-path


    【解决方案1】:

    您应该有一个数组来跟踪文件路径,并且该函数应该将文件路径添加到数组中。然后在同一个函数或者最好是一个单独的函数中循环遍历数组并包含每个文件。

    所以在你的函数之外你会定义一个新的数组,这里我在会话中做它以便可以很容易地访问它。

    if (!isset($_SESSION['files'])) {
        $_SESSION['files'] = []; // or array() if your PHP version is < 5.4
    }
    

    现在在你的函数中改变这一行:

    include "load_more/newfile $variable .php";
    

    到这里:

    $_SESSION['files'][] = "load_more/newfile $variable .php";
    

    现在循环遍历文件数组并包含每个数组。这可以在 generateNewPage 函数中完成,也可以在单独的函数中完成,但从 generateNewPage 函数中调用。

    foreach ($_SESSION['files'] as $file) {
        include $file;
    }
    

    希望这很清楚。如果您有任何问题,请告诉我。

    【讨论】:

    • 谢谢@David Jones,我有一个小问题。按照我放置您建议的代码的顺序,它似乎无法正常工作(如上面的编辑所示)。有什么建议吗?
    • 我写的代码顺序错了吗(在编辑上方)
    • 我的代码中有错字。检查我的编辑。此外,代码未经测试,只是说明我的解决方案的指南。如果它适合你,你应该尝试扩展这个想法,并意识到为什么你的原始代码没有达到你的期望。如果您需要更多解释,请告诉我。
    猜你喜欢
    • 2015-04-21
    • 2019-11-28
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    相关资源
    最近更新 更多