Here is my code
<?php
$myFile = "hidata.txt";
$fh = fopen($myFile, 'r');
$counter = 1;
$page = 2;
while(false !== ($theData = fgets($fh))) {
if ( $counter > 10){
file_put_contents( '/i', $page.".php", '<li>' . $theData . '</li>' . PHP_EOL, FILE_APPEND );
if ($counter == 20){
$counter=10;
$page++;
}
}else{
echo ('<li>' . $theData . '</li>');
}
$counter++;
}
fclose($fh);
?>
This code automatically generates new .php pages everytime the hidata.txt
reaches more than 10,20,30... lines of texts. I want every generated pages to automatically have same header, footer and layout. How can I do that in this code?
Edit: I tried it but not working
<?php
$myFile = "hidata.txt";
$fh = fopen($myFile, 'r');
$counter = 1;
$page = 2;
$newpage = true;
while(false !== ($theData = fgets($fh))) {
if ( $counter > 10){
if ($newpage) {
file_put_contents(..... $header .....);
$newpage = false;
}
file_put_contents( '/i', $page.".php", '<li>' . $theData . '</li>' . PHP_EOL, FILE_APPEND );
if ($counter == 20){
$counter=10;
$page++;
$newpage = true;
}
}else{
echo ('<li>' . $theData . '</li>');
}
$counter++;
}
fclose($fh);
?>
Edit 2: Here is my hidata.txt
data1
data2
data3
data4
data5
and so on
And here is my header.php
<h1>Test</h1>
[edit - sample code]
$page = 2;
$newpage = true;
while(false !== ($theData = fgets($fh))) {
if ( $counter > 10){
if ($newpage) {
file_put_contents(..... $header .....);
$newpage = false;
}
file_put_contents( '/i', $page.".php", '<li>' . $theData . '</li>' . PHP_EOL, FILE_APPEND );
if ($counter == 20){
$counter=10;
$page++;
$newpage = true;
}
[original]
You could add code something like this:
if ( $counter > 10){
/********** YOUR CODE HERE *****/
if (first putfor this page) {
file_put_contents('/i',$page.".php",$header . PHP_EOL, FILE_APPEND);
}
/******* *****/
file_put_contents( '/i', $page.".php", '<li>' . $theData . '</li>' . PHP_EOL, FILE_APPEND );
Similary, append the "footer" when you $page++;
. You'll have to check before the fclose
that the final footer was written.
$header
could be a string like require_once("header.php")
, or the result of a file_get_contents
, depending on what you need. Ditto the footer.
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.