Home >Backend Development >PHP Tutorial >Detailed tutorial on generating static pages in php

Detailed tutorial on generating static pages in php

WBOY
WBOYOriginal
2016-07-25 09:05:47978browse
  1. { title }
  2. this is a { file } file's templets
Copy code

PHP processing: templatetest.php

  1. $title = "Test Template";
  2. $file = "TwoMax Inter test templet,author:Matrix@Two_Max";
  3.  $fp = fopen ("temp.html","r ");
  4. $content = fread ($fp,filesize ("temp.html"));
  5. $content .= str_replace ("{ file }",$file,$content);
  6. $content .= str_replace (" { title }",$title,$content);
  7. echo $content;
  8. ?>
Copy code

  Template parsing processing, that is, filling (content) with the results obtained after PHP script parsing and processing Template processing process. Usually with the help of template classes. Currently, the more popular template parsing classes include phplib, smarty, fastsmarty and so on. The principle of template parsing processing is usually replacement. There are also some programmers who are accustomed to putting judgment, looping and other processing into template files and processing them with parsing classes. The typical application is the block concept, which is simply a loop processing. The PHP script specifies the number of loops, how to loop through, etc., and then the template parsing class implements these operations.

 How to generate static files with PHP.

  PHP generating static pages does not refer to PHP’s dynamic parsing and outputting HTML pages, but refers to using PHP to create HTML pages. At the same time, because of the non-writable nature of HTML, if the HTML we create is modified, it needs to be deleted and regenerated. (Of course, you can also choose to use regular rules to modify it, but I personally think that it is faster than deleting and regenerating it, which is not worth the gain.)

PHP fans who have used PHP file operation functions know that there is a file operation function fopen in PHP, which is to open a file. If the file does not exist, try to create it. This is the theoretical basis on which PHP can be used to create HTML files. As long as the folder used to store HTML files has write permission (ie permission definition 0777), the file can be created. (For UNIX systems, Win systems do not need to be considered.) Taking the above example as an example, if we modify the last sentence and specify to generate a static file named test.html in the test directory:

  1. $title = "Test Template";
  2. $file = "TwoMax Inter test templet,author:Matrix@Two_Max";
  3. $fp = fopen ("temp.html","r ");
  4. $content = fread ($fp,filesize ("temp.html"));
  5. $content .= str_replace ("{ file }",$file,$content);
  6. $content .= str_replace (" { title }",$title,$content);
  7. // echo $content;
  8. $filename = "test/test.html";
  9. $handle = fopen ($filename,"w"); //Open file pointer , create a file
  10. /*
  11. Check whether the file is created and writable
  12. */
  13. if (!is_writable ($filename)){
  14. die ("File: ".$filename." is not writable, please check its properties and try again Try! ");
  15. }
  16. if (!fwrite ($handle,$content)){ //Write information to the file
  17. die ("Generate file".$filename."Failed!");
  18. }
  19. fclose ( $handle); //Close the pointer
  20. die ("Create file".$filename."Success!");
  21. ?>
Copy code

Reference for solutions to common problems: 1. Article list issues: Create a field in the database and record the file name. Each time a file is generated, the automatically generated file name is stored in the database. For recommended articles, just point to the page in the designated folder where the static files are stored. Use PHP operations to process the article list, save it as a string, and replace this string when generating the page. For example, add the tag {articletable} to the table where the article list is placed on the page, and in the PHP processing file:

  1. $title = "Test Template";
  2. $file = "TwoMax Inter test templet,author:Matrix@Two_Max";
  3. $fp = fopen ("temp.html","r ");
  4. $content = fread ($fp,filesize ("temp.html"));
  5. $content .= str_replace ("{ file }",$file,$content);
  6. $content .= str_replace (" { title }",$title,$content);
  7. //Start generating list
  8. $list = '';
  9. $sql = "select id,title,filename from article";
  10. $query = mysql_query ($sql);
  11. while ($result = mysql_fetch_array ($query)){
  12. $list .= ''.$result['title'].'';
  13. }
  14. $content .= str_replace ("{ articletable }",$list, $content);
  15. //End of generating list
  16. // echo $content;
  17. $filename = "test/test.html";
  18. $handle = fopen ($filename,"w"); //Open the file pointer and create File
  19. /*
  20. Check whether the file is created and writable
  21. */
  22. if (!is_writable ($filename)){
  23. die ("File: ".$filename." is not writable, please check its properties and try again! ");
  24. }
  25. if (!fwrite ($handle,$content)){ //Write information to the file
  26. die ("Generate file".$filename."Failed!");
  27. }
  28. fclose ($handle ); //Close the pointer
  29. die ("Create file".$filename."Success!");
  30. ?>
Copy code

Second, paging problem. ​If we specify pagination, there will be 20 articles per page. There are 45 articles in a certain sub-channel list according to the database query. First, we obtain the following parameters through query: 1, the total number of pages; 2, the number of articles per page. The second step, for ($i = 0; $i

  1. $fp = fopen ("temp.html","r");
  2. $content = fread ($fp,filesize ("temp.html"));
  3. $onepage = '20';
  4. $sql = "select id from article where channel='$channelid'";
  5. $query = mysql_query ($sql);
  6. $num = mysql_num_rows ($query);
  7. $allpages = ceil ($num / $onepage);
  8. for ($i = 0;$i<$allpages; $i++){
  9. if ($i == 0){
  10. $indexpath = "index.html";
  11. } else {
  12. $indexpath = "index_".$i."html";
  13. }
  14. $start = $i * $onepage;
  15. $list = '';
  16. $sql_for_page = "select name,filename,title from article where channel='$channelid ' limit $start,$onepage";
  17. $query_for_page = mysql_query ($sql_for_page);
  18. while ($result = $query_for_page){
  19. $list .= ''.$title.'';
  20. }
  21. $content = str_replace ("{ articletable }",$list,$content);
  22. if (is_file ($indexpath)){
  23. @unlink ($indexpath); //If the file already exists, delete it
  24. }
  25. $handle = fopen ($ indexpath,"w"); //Open the file pointer and create the file
  26. /*
  27. Check whether the file is created and writable
  28. */
  29. if (!is_writable ($indexpath)){
  30. echo "File: ".$indexpath ."Not writable, please check its properties and try again!"; //Change to echo
  31. }
  32. if (!fwrite ($handle,$content)){ //Write information to the file
  33. echo "Generate file". $indexpath."Failed!"; //Change to echo
  34. }
  35. fclose ($handle); //Close pointer
  36. }
  37. fclose ($fp);
  38. die ("Generation of paging file is completed. If the generation is incomplete, please Check the file permission system and then regenerate! ");
  39. ?>
Copy code

Other data generation, data input and output checking, paging content pointing, etc. can be added to the page as appropriate.

Articles you may be interested in: Three methods and code details for generating static pages in PHP Example of php generating static page function (php2html) How to generate static pages in php (three functions) Details on templates and caching of static files generated by PHP A class written in php to generate static pages How to automatically generate static pages on a virtual host at regular intervals Two ways to generate static files with php Principle analysis of php generating static html files How to generate static pages using smarty Understand the principle of php generating static HTML files How to generate static pages with PHP Three ways to generate static html files with php



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn