[英]How can I automatically put same header, footer and layout to every pages generated by this PHP code?
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. 每当
hidata.txt
超过10,20,30 ...行文本时,此代码就会自动生成新的.php页面。 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 编辑2:这是我的hidata.txt
data1
data2
data3
data4
data5
and so on 等等
And here is my header.php 这是我的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++;
类似地,在
$page++;
时附加“页脚” $page++;
. 。 You'll have to check before the
fclose
that the final footer was written. 在
fclose
之前,您必须检查是否已写入最后的页脚。
$header
could be a string like require_once("header.php")
, or the result of a file_get_contents
, depending on what you need. $header
可以是类似于require_once("header.php")
的字符串,也可以是file_get_contents
的结果,具体取决于您的需求。 Ditto the footer. 同上页脚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.