Html templates generate static pages and templates for pagination. templates for static pages

Source: Internet
Author: User

Html templates generate static pages and templates for pagination. templates for static pages

It only allows you to modify a part of the page. Of course, this "part" is determined by you. The artist first made a page and then treated the page as a template (note that this template does not need to use code such as EditRegion3, this kind of code is the identifier that Dreamwerver gets to facilitate its own design). We will replace what we need to change in this template with a character that can be distinguished from HTML, for example, "{title}" and "[title]". When generating a static page, you only need to replace the data with these strings. This is the meaning of the template.

Create a php page and an html page [template page]. Note: if data is called from the database, save the data as an array and generate it cyclically;
On the php page, open the html page-> read the content of the html page-> Replace the parameter-> Create (open) A new html page-> write the replaced content to the new file-> close the new file-> Generate successfully;

$ Open = fopen ("template.htm", "r"); // open the Template File $ content = fread ($ open, filesize ("template.htm ")); // read the template file content // print_r ($ content); $ content = str_replace ("{title}", "test title", $ content ); // Replace $ content = str_replace ("{contents}", "Test content", $ content); $ newtemp = fopen ("1.htm"," w "); // generate, use the write method to open a non-existing (new) page fwrite ($ newtemp, $ content); // write the replaced content to the new file fclose ($ newtemp ); echo "generate ";

Php batch generate html test:

// Assume that the data transferred from the database is stored in a two-dimensional array $ arr = array ("News Title 1", "news content 1 "), array ("News title 2", "news content 2"); foreach ($ arr as $ key => $ value) {$ title = $ value [0]; $ contents = $ value [1]; // echo $ title. ''. $ contents. ''; $ path = export key.'.html '; $ open = fopen (" template.htm "," r "); // open the Template File $ handle = fread ($ open, filesize ("template.htm"); // read the template file content $ content = str_replace ("{title}", $ title, $ handle ); // Replace $ content = str_replace ("{contents}", $ contents, $ handle); $ newtemp = fopen ($ path, "w "); // use the write method to open a non-existing (new) page fwrite ($ newtemp, $ content); // write the replaced content to the new file fclose ($ newtemp ); echo "generate ";}

Paging problem:

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:

$ 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 _f Or_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. = '<a href = '. $ root. $ result ['filename']. 'Target = _ blank> '. $ title. '</a> <br>';} $ 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 the file if (! Is_writable ($ indexpath) {echo "file:". $ indexpath. "cannot be written. Check its attributes and try again! "; // Modify to echo} if (! Fwrite ($ handle, $ content) {// write information to the file echo "generate File". $ indexpath. "failed! "; // Modify to echo} fclose ($ handle); // close pointer} fclose ($ fp); die (" generation of paging files is complete. If generation is incomplete, check the File Permission System and generate a new one! ");

 

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.