Php static page generation method and implementation code version_php tutorial

Source: Internet
Author: User
Php provides a detailed method and implementation code for generating static pages. Fread () and fwirte () are used in php (). After a static page is generated, the modification will be involved. Here, we can use the regular expression matching method to replace the template. in php, we mainly use fread () and fwirte (). After a static page is generated, the modification will be involved. Here we can use the regular expression matching method to replace the changed parts in the template. However, this method is too troublesome. the recommended method is to cut down the original template and generate it again.
It should also be noted that this method of generating static pages is generally used for pages that are not frequently changed, such as the final page of information. For list pages, it is also advisable to update information frequently. Currently, many blogs or forum programs that can generate static pages are popular on the internet. they are "semi-automatic" html generation by manually clicking the "generate html page" button in the background. However, portal websites with a large amount of information do not work. Static pages are called static pages because they cannot be changed automatically. If the information list is updated 100 times a day, the static list page will be re-generated 100 times. If I have 10 such columns, I think it's enough to vomit blood.
Now let's take a look at the actual program demonstration:
First: it is implemented using ob functions. the code is relatively simple and the efficiency is relatively high.

The code is as follows:


Ob_start ();
@ Readfile ("http://tools.jb51.net /");
$ Text = ob_get_flush ();
$ Myfile = fopen ("myfile.html", "w ");
$ Text =
Str_replace ("{counent}", $ string, $ text );
Fwrite ($ myfile, $ text );
Ob_clean ();
?>


Because even if you want to generate a static page, dynamic reading of that part should also be retained. after the data is inserted into the database, the url is passed to the readfile function and then read into the cache, fwrite can generate static pages at a moment. this is a kind of method most appreciated by camels. The minimum number of lines of code is the most efficient. Http://tools.jb51.net/is the content of a bare page. In this way, we can compare the free template myfile.html. If you only need to generate static pages, this will basically meet your needs.
Second: generate a static html page.
This is done step by step. fread comes into the page and then replaces str_replace
First, create the final content page:
PHP code

The code is as follows:


$ Title = "http://siyizhu.com test template ";
$ File = "TwoMax Inter test templet,
Author: [email = Matrix @ Two_Max] Matrix @ Two_Max [/email] ";
$ Fp = fopen ("temp.html", "r ");
$ Content = fread ($ fp, filesize ("temp.html "));
$ Content = str_replace ("{file}", $ file, $ content );
$ Content = str_replace ("{title}", $ title, $ content );
$ Filename = "test/test.html ";
$ Handle = fopen ($ filename, "w"); // open the file pointer and create a file
/* Check whether the file is created and writable */
If (! Is_writable ($ filename ))
{
Die ("File:". $ filename. "cannot be written. check its attributes and try again! ");
}
If (! Fwrite ($ handle, $ content ))
{// Write information to a file
Die ("generate file". $ filename. "failed! ");
}
Fclose ($ handle); // Close the pointer
Die ("Creating File". $ filename. "successful! ");
?>


This step is relatively simple. Just replace the variables. If you want to generate a static list page, the principle is the same. you can use a program to generate a list of articles and use it as a large variable to replace the variables in the template. this is the case when you flip the list. Of course, if there is any information update, the list will be re-generated by turning pages.
PHP code

The code is as follows:


$ Title = "http ://";
$ File = "TwoMax Inter test templet,
Author: [email = Matrix @ Two_Max] Matrix @ Two_Max [/email] ";
$ Fp = fopen ("temp.html", "r ");
$ Content = fread ($ fp, filesize ("temp.html "));
$ Content = str_replace ("{file}", $ file, $ content );
$ Content = str_replace ("{title}", $ title, $ content );
// Starts generating the list
$ List = '';
$ SQL = "select id, title, filename from article ";
$ Query = mysql_query ($ SQL );
While ($ result = mysql_fetch_array ($ query ))
{
$ List. = ''. $ result ['title'].'
';
}
$ Content. = str_replace ("{articletable}", $ list, $ content); // The Generation list ends.
// Echo $ content;
$ Filename = "test/test.html ";
$ Handle = fopen ($ filename, "w ");
// Open the file pointer and create a file
/* Check whether the file is created and writable */
If (! Is_writable ($ filename ))
{
Die ("File:". $ filename. "cannot be written. check its attributes and try again! ");
}
If (! Fwrite ($ handle, $ content ))
{// Write information to a file
Die ("generate file". $ filename. "failed! ");
}
Fclose ($ handle); // Close the pointer
Die ("Creating File". $ filename. "successful! ");
?>


Page turning:
For example, we specify 20 pages per page. If the number of articles in a subchannel list is 45 in the database, we first obtain the following parameters through the query: 1, the total number of pages; 2, the number of entries per page. Step 2: for ($ I = 0; $ I <allpages; $ I ++), page element acquisition, analysis, and article generation are all executed in this loop. The difference is that die ("create file". $ filename. "succeeded! "; This sentence is removed and displayed after the loop, because the statement will stop the program execution.
Example:
PHP code

The code is as follows:


$ 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 );
$ Num = mysql_num_rows ($ query );
$ Allpages = ceil ($ num/$ onepage );
For ($ I = 0; $ I <$ 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 ("{articletable}", $ list, $ content );
If (is_file ($ indexpath ))
{
@ Unlink ($ indexpath); // delete a file if it already exists
}
$ Handle = fopen ($ indexpath, "w"); // open the file pointer and create a file
/* Check whether the file is created and writable */
If (! Is_writable ($ indexpath ))
{
Echo "File:". $ indexpath. "cannot be written. check its attributes and try again! "; // Modify it to echo
}
If (! Fwrite ($ handle, $ content ))
{// Write information to a file
Echo "generating file". $ indexpath. "failed! "; // Modify it to echo
}
Fclose ($ handle); // Close the pointer
}
Fclose ($ fp );
Die ("Generation of paging files is complete. if the generation is incomplete, check the file permission system and generate a new one! ");
?>


Third: static page generated by the smarty Template
Smarty has a fetch function, which is similar to fread () and can be used to generate static pages.
This example seems familiar to everyone. right, the example of the fetch function in the smarty manual is always more classic than the official example!
PHP code

The code is as follows:


Include ("Smarty. class. php ");
$ Smarty = new Smarty;
$ Smarty-> caching = true;
// Only do db CILS if cache doesn't exist
If (! $ Smarty-> is_cached ("index. tpl "))
{// Dummy up some data
$ Address = "245 N 50th ";
$ Db_data = array ("City" => "Lincoln", "State" => "Nebraska", "Zip" => "68502 ");
$ Smarty-> assign ("Name", "Fred ");
$ Smarty-> assign ("Address", $ address );
$ Smarty-> assign ($ db_data );
} // Capture the output
$ Output = $ smarty-> fetch ("index. tpl ");
// This is the key. // do something with $ output here
Echo $ output; // If hoho sees the output result, then? Fwrite, and we will get the result we want.
$ Fp = fopen ("archives/2005/05/19/0001 .html", "w ");
Fwrite ($ fp, $ content );
Fclose ($ fp );
?>


PHP code

The code is as follows:


Ob_start ();
Echo "Hello World! ";
$ Content = ob_get_contents (); // retrieves all the content output on the php page.
$ Fp = fopen ("archives/2005/05/19/0001 .html", "w ");
Fwrite ($ fp, $ content );
Fclose ($ fp );
?>

Vertex () and fwirte (). After a static page is generated, the modification will be involved. Here we can use the regular expression matching method to replace the changes in the template...

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.