简体   繁体   中英

PHP include file - overriding itself in function

Here is my code:

 <?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> 

Basically, every time that the user clicks a button a new page is generated via a session adding 1 each time to the fopen.

When the user clicks the button, a page is generated, then that page is included.

However, every time that the function is ran, the include statement overrides itself and only the newest of the newly generated pages is included.

I want every generated page to be included incrementally as the user clicks the button.

Is this possible? And if so then how?

Updates for @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> 

You should have an array to keep track of the file paths and the function should add the file path to the array. Then in the same function or preferably a separate function you would loop through the array and include each file.

So outside your function you would define a new array, here I am doing it in the session so it can be accessed easily.

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

Now in your function change this line:

include "load_more/newfile $variable .php";

To this:

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

Now loop through the files array and include each one. This can be done in the generateNewPage function or in a separate function but called from the generateNewPage function.

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

Hope this is clear. Let me know if you have any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM