A detailed tutorial on generating static pages in PHP

Source: Internet
Author: User
Tags fread php write
    1. {title}
    2. This is a {file} file ' s templets
Copy Code

PHP Processing: templetest.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, will be after the PHP script parsing processing results of filling (content) into the template processing process. Usually with the help of a template class. At present, the more popular template parsing class has phplib,smarty,fastsmarty and so on. The principle of template parsing processing is usually a replacement. Also some programmers are accustomed to judge, loop and other processing into the template file, with the Analytic class processing, the typical application for the block concept, simply for a loop processing. The PHP script specifies the number of cycles, how to iterate, and so on, which is implemented by the template parsing class.

How to generate static files in PHP.

PHP generated static page does not refer to the dynamic parsing of PHP, the output of HTML pages, but the use of PHP to create HTML pages. At the same time, because the HTML is not writable, we create the HTML if there are changes, we need to delete the regeneration. (Of course, you can also choose to use the regular to modify, but personally think that does not make it easier to delete the regeneration is faster, some outweigh the loss.) )

PHP Fans with PHP file operation function know that PHP has a file operation function fopen, that is, open the file. If the file does not exist, try to create it. This is the theoretical basis for PHP to create HTML files. The file can be created as long as the folder where the HTML file is stored has write permissions (that is, permission definition 0777). (For Unix systems, the win system does not need to be considered.) Still, for example, if we modify the last sentence and specify that a static file named test.html be generated 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 file
    10. /*
    11. Check if the file is created and writable
    12. */
    13. if (!is_writable ($filename)) {
    14. Die ("File:". $filename. " Not writable, please check its properties and try again! ");
    15. }
    16. if (!fwrite ($handle, $content)) {//write information to file
    17. Die ("Generate file". $filename. " Failed! ");
    18. }
    19. Fclose ($handle); Close pointer
    20. Die ("Create file". $filename. " Success! ");
    21. ?>
Copy Code

Frequently Asked Questions solution reference: First, the article List problem: Create a field in the database, record the file name, each generated a file, the auto-generated file name into the database, for the recommended article, just point to the static file in the specified folder of the page. Use PHP to manipulate the list of articles, save as a string, and replace this string when generating a page. For example, the table that places the list of articles in the page is added with the tag {articletable}, 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. Build list Start
  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 Build list
  16. Echo $content;
  17. $filename = "test/test.html";
  18. $handle = fopen ($filename, "w"); Open file pointer, create file
  19. /*
  20. Check if the file is created and writable
  21. */
  22. if (!is_writable ($filename)) {
  23. Die ("File:". $filename. " Not writable, please check its properties and try again! ");
  24. }
  25. if (!fwrite ($handle, $content)) {//write information to file
  26. Die ("Generate file". $filename. " Failed! ");
  27. }
  28. Fclose ($handle); Close pointer
  29. Die ("Create file". $filename. " Success! ");
  30. ?>
Copy Code

Two, paging problem. If we specify paging, 20 articles per page. A sub-channel list of the article through the database query for 45, then, we first through the query to obtain the following parameters: 1, the total number of pages, 2, per page. The second step, for ($i = 0; $i < allpages; $i + +), page element acquisition, analysis, article generation, are executed in this loop. The difference is that die ("Create file". $filename. " Success! This sentence is removed and placed on the display after the loop, because the statement aborts the execution of the program. Cases:

  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 the
  24. }
  25. $handle = fopen ($indexpath, "w"); Open file pointer, create file
  26. /*
  27. Check if 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! "; Modify to Echo
  31. }
  32. if (!fwrite ($handle, $content)) {//write information to file
  33. echo "Generate file". $indexpath. " Failed! "; Modify to Echo
  34. }
  35. Fclose ($handle); Close pointer
  36. }
  37. Fclose ($FP);
  38. Die ("Raw ingredient page file complete, such as build incomplete, please check file permissions system after rebuild!") ");
  39. ?>
Copy Code

such as other data generation, data input and output inspection, paging content points, etc. can be added to the page as appropriate.

you may be interested in the article:three ways to generate static pages in PHP and the code in detail PHP generates static page functions (php2html) example PHP method for generating static pages (three functions) The template of PHP generated static files and cache PHP write a static page generated on the virtual host on a regular basis, two ways to generate static files PHP php generated static HTML file principle analysis Smarty Generate static page method php generation static HTML file Method of the page PHP three ways to generate a static HTML file

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.