简体   繁体   English

如何自动将相同的页眉,页脚和布局放到此PHP代码生成的每个页面上?

[英]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.

相关问题 如何在Codeigniter中将视图页面与相同的页眉和页脚组合在一起? - How can I combine view pages with the same header and footer in Codeigniter? 如何在不同页面上显示相同的页眉和页脚? - How to display same header and footer on different pages? 如何在wordpress的header.php和footer.php中使用相同的随机生成值 - How to use same randomly generated value in header.php and footer.php in wordpress 如何在组件中保持页眉和页脚相同,内容将被更改 - How can I keep the header and footer the same in components and content will be changed 如何在子域的每个 html/php 页面中自动添加 html header? - How to add html header automatically, in every single html/php pages of the subdomain? 如何在 shell 脚本或 PHP 中每 30 分钟自动运行一次代码? - How can i make a code run automatically every 30 mins in shell script or PHP? 我如何为所有特定的 php 页面放置 css - How can i put the css for all specific php pages 如何在某些页面的页眉中包含代码段? - How can I include a code snippet in header of certain pages? 如何自动加载页眉和页脚视图? - How do I automatically load a header and footer view? 我如何从wordpress主题目录中使用header.php和footer.php - how I can use header.php and footer.php outside from wordpress theme directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM