-------------------------------------------------- --------------------------------------------
Copyright Notice: You can reprint anything, please be sure to indicate the original source and author information and this statement by hyperlink.
This article comes from: http: //www.otm.cn Author: Matrix @ Two_Max
Let's take a look at some basic concepts. First, PHP scripts and dynamic pages. The PHP script is a server-side scripter that can be mixed with the HTML file by embedding or other methods, or the user requests can be processed in the form of a class, a function package. Regardless of the way, its basic principle is like this. Proposed by the client, request a page -----> web server to introduce the specified script for processing -----> script is loaded into the server ----> The PHP parser specified by the server pair scripts Perform parsing to form an HTML language form ----> Transfer the parsed HTML statements into the browser in a package. It is not difficult to see that after the page is sent to the browser, PHP does not exist, and has been converted to an HTML statement. The customer request is a dynamic file, in fact, there is no real file exists there, is a PHP resolution to a corresponding page, and then send back the browser. This page processing method is called "dynamic page". Second, the static page. The static page refers to a page that only includes only HTML and JS, CSS, and other clients that exist only in the server side. Its processing method is. Request by the client, request a page ----> web server confirmation and load a page ----> web server passes the page back to the browser in the form of a package. From this process, we compare the dynamic page, that is, it is available. The dynamic page needs to be parsed by the PHP parser of the web server, and the database is often required to perform the data inventory, and then the HTML language packet can be formed; and the static page, no resolution, no need to connect the database, it can be sent directly. Redightly reduce server pressure, improve server load capacity, and greatly provide page open speed and website overall open speed. However, its disadvantage is that the request must not be dynamically processed, and the file must be existed on the server. Third, template and template analysis. The template has not yet filled the content HTML file. For example: Temp.html
CODE:
PHP processing:
Templetest.php
CODE:
$ title = "PHP enthusiast test template"; $ file = "twomax interface test templet, author: sheyi"; $ fp = fopen ("Temp.html", "R"); $ Content = FREAD ($ FP, FILSIZE ("Temp.html"))); $ content. = STR_REPLACE ("{file}", $ file, $ content); $ content. = STR_REPLACE ("{title}", $ TITLE, $ Content); Echo $ Content ;?>
Template parsing processing, that is, the resulting result of the result fill after the PHP script parsing process is processed. Usually by means of template classes. Currently popular template parsing classes include phplib, smarty, fastsmarty, and more. The principle of template parsing processing is usually replaced. Some programmers habits will judge, looped, etc., in the template file, use the parsing class, typically apply to the Block concept, simply as a loop process. The number of cycles is specified by the PHP script, how to loop into the form, and then the template resolution class is specifically implemented. Ok, contrast the static pages and dynamic pages, now let's talk about how to generate static files with PHP.
PHP generation static page does not refer to PHP dynamic resolution, output HTML pages, but refers to the HTML page with PHP. At the same time, because HTML is unpackable, if there is a modification, if there is a modification, you can delete the regeneration. (Of course, you can also choose to modify it with regular, but individuals think that it is better to delete and regenerate, and some will not pay.)
The words retired. PHP Fans with PHP file operation functions know that there is a file operation function fopen in PHP, that is, opens the file. If the file does not exist, try creating. This is the theoretical basis for PHP can be used to create an HTML file. As long as the folder with the HTML file is written (ie, the privilege definition 0777) can create a file. (For UNIX systems, Win systems do not need to be considered.) Still in the example, if we modify the last sentence, and specify a static file called Test.html in the Test directory:
CODE:
$ Title = "Tutans International Test Template"; $ file = "Twomax Inter Test Templet, Author: Matrix @ Two_max"; $ fp = fopen ("Temp.html", "R"); $ Content = FREAD ($ FP) FileSize ("temp.html"); $ content. = STR_REPLACE ("{file}", $ file, $ content); $ content. = str_replace ("{title}", $ title, $ content; / / Echo $ Content; $ filename = "test.html"; $ handle = fopen ($ filename, "w"); // Open the file pointer, create a file / * Check if the file is created and writable * / if (! is_writable)) {Die ("File:". $ filename. "Do not be written, please check after its properties!");} if (! fwrite ($ handle, $ content) {//// Write the information to file DIE ("Generate File". $ Filename. "Failed!");} Fclose ($ hand); // Close the pointer Die ("Create File". $ Filename. "Success!");?>>
FAQ in practical applications Solutions Reference:
First, the article list question:
Create a field in the database, record the file name, each generation of a file, store the automatically generated file name into the database, for the recommended article, simply point to the page in the specified folder stored in the static file. Use the PHP operation to process the article list, save the string, and replace this string when generating a page. For example, place the table list in the page to add tag {Articles}, and in the PHP processing file: Code:
$ Title = "Tutans International Test Template"; $ file = "Twomax Inter Test Templet, Author: Matrix @ Two_max"; $ fp = fopen ("Temp.html", "R"); $ Content = FREAD ($ FP) FileSize ("temp.html"); $ content. = STR_REPLACE ("{file}", $ file, $ content); $ content. = str_replace ("{title}", $ title, $ content; / / Generate list start $ list = ''; $ SQL = "Select ID, Title, FileName from Article"; $ query = mysql_query ($ SQL); WHILE ($ RESULT = mysql_fetch_array) {$ list. = ' '. $ Result [' Title '].' ';} $ content. = STR_REPLACE ("{articles", $ list, $ content); // Generate list End // Echo $ content; $ filename = "TEST / Test.html "; $ handle = fopen ($ filename," w "); // Open the file pointer, create a file / * Check if the file is created and writable * / if (! is_writable ($ filename) {Die ( "File:" $ filename. "Do not be written, please check after its property!");} If (! Fwrite ($ handle, $ content)) {// write information into file DIE ("Generate File" "Fail!");} fclose ($ hand); // Turns the pointer Die ("Create File". $ filename. "Success!");?>>
Second, paging problems.
20 pieces per page as we specify pagination. The article inside a sub-channel is 45, then, first we get the following parameters by query: 1, the total number of pages; 2, the number of each page. Step 2, for ($ I = 0; $ I $ fp = fopen ("Temp.html", "R"); $ Content = FREAD ($ FP, FileSize ("Temp.html")); $ ONEPAGE = '20'; $ SQL = "Select ID from Article Where Channel = '$ CHANNELID' "; $ query = mysql_query ($ sql); $ number = mysql_num_rows ($ coluery); $ NUM / $ 00; $ 11; $ 11 $ allpages $ I ) {IF ($ I == 0) {$ indexpath = "index.html";} else {$ indexpath = "index _". $ i. "html";} $ start = $ i * $ onepage; $ list = ''; $ sql_for_page = "select name, filename, title from article where channel = '$ channelid' limit $ start, $ onepage"; $ query_for_page = mysql_query ($ sql_for_page); while ($ result = $ query_for_page) {$ List. = ''. $ Title. '';} $ Content = str_replace ("{articles); if (is_file) {@unlink ($ indexpath); / / If the file already exists, delete} $ handle = fopen ($ indexpath, "w"); // Open the file pointer, create a file / * Check if the file is created and writable * / if (! Is_writable ($ indexpath) ) { Echo "Document:" $ indexpath. "Do not be written, please check after its properties! "; // Modify to echo} if (! Fwrite ($ handle, $ content) {// write information into file echo" generated file ". $ Indexpath." Failed! "; // Modify it to echo} fclose ($ handle); // Close pointer} fclose ($ fp); Die (" Generate paging files complete, if the generated is incomplete, please check the file permission system to regenerate! "); ?> Better thinking, such as other data generation, data input output check, paging content points, etc., may be added to the page as appropriate.