Home > Backend Development > PHP Tutorial > Code to generate html static pages from all content in the database

Code to generate html static pages from all content in the database

WBOY
Release: 2016-07-25 09:05:51
Original
1389 people have browsed it
  1. require_once("conn.php");
  2. if($_GET['all'])
  3. {
  4. /*Get the database record so that the html file has a file name*/
  5. $sqlquery = "select * from $tbname";
  6. $result = mysql_query($sqlquery,$conn)or die("Query failed!");
  7. $fp = fopen("./template/article.html", r);
  8. $fpcontent = fread($fp,filesize("./template/article.html"));
  9. fclose($fp);
  10. /*Write file*/
  11. while($row = mysql_fetch_array($ result))
  12. {
  13. $fpcontent = str_replace("{thetitle}",$row['title'],$fpcontent);
  14. $fpcontent = str_replace("{chatitle}",$row['title'],$ fpcontent);
  15. $fpcontent = str_replace("{bookcontent}",$row['content'],$fpcontent);
  16. $fp = fopen("./html/".$row['id'].". html",w)or die("Failed to open the file for writing!");
  17. fwrite($fp,$fpcontent)or die("Failed to write the file!");
  18. }
  19. echo "";
  20. }
  21. if($_GET['part'])
  22. {
  23. /*Get the ID of the last record to facilitate the generation of html files. File name*/
  24. $sqlquery = "select * from $tbname order by id desc limit 1";
  25. $result = mysql_query($sqlquery,$conn)or die("Query failed!");
  26. $row = mysql_fetch_array( $result);
  27. $fp = fopen("./template/article.html",r);
  28. $fpcontent = fread($fp,filesize("./template/article.html"));
  29. fclose($ fp);
  30. $fpcontent = str_replace("{thetitle}",$row['title'],$fpcontent);
  31. $fpcontent = str_replace("{chatitle}",$row['title'],$fpcontent) ;
  32. $fpcontent = str_replace("{bookcontent}",$row['content'],$fpcontent);
  33. $fp = fopen("./html/".$row['id'].".html" ,w)or die("Failed to open the file for writing!");
  34. fwrite($fp,$fpcontent)or die("Failed to write the file!");
  35. echo "";
  36. }
  37. ?>
  38. Generate html
  39. echo "All updated
    Partial update";
  40. ?>
Copy code

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 Detailed tutorial on generating static pages 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



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template