PHP static page _php tutorial for generating HTML static pages using PHP

Source: Internet
Author: User

HTML static page generated using PHP implementation, PHP static page


Generates HTML static pages from PHP and stores them in a directory created with the name of the year and month.

Read all data batch generation, all generated after the popup prompt.

You can specify the number of batches to be generated, no more than 800 is recommended, or the execution speed will be problematic.

(For well-known reasons, the data field name involved in the database has been changed and the part of the parameter filter has been removed for code clarity)

Description: The original dynamic address is moban.php?id=1, the post-generated address is html/200808/sell_1.html. Page.php is a paging program, published in this blog.

Page usage, save this code as make.php, using the method for the browser to access the number of make.php?t= &pg= page, such as make.php?t=300&pg=2, that is, each time 300 data generated, starting from the 2nd page of the data list, That is, skip the previous 300 bars. If you access make.php directly without any parameters, the default is 200 per build, starting from the first page.

Complete Example:

<?php if ($_get[pg]== ') {$aa = 1;} else{$aa =$_GET[PG];} include ("admin/conn.php"); Require_once ("page.php"); $result =mysql_query ("SELECT * from 2carsell"); $totle =mysql_num_rows ($result); $pagelist = $_get[t]; if ($_get[t]== ') {$pagelist = ' 200 ';} else{$pagelist =$_get[t];} $pager = new Pager ($totle, $pagelist); $datastat = "Total". $pager->countall."Bar, each time you generate". $pager->countlist."Bar, total build required". $pager->page."//Data Statistics $BB = $pager->page; $pagenav = $pager->backstr. $pager->thestr. $pager->nextstr; $limitFrom = $pagelist * ($pager->pg-1); $result =mysql_query ("SELECT * from 2carsell the ORDER by ID DESC limit $limitFrom, $pagelist");?>
 
  section <?echo $aa?> page Generation: <? echo $datastat?>
  
<? PHP generates static page print " "; while ($datauser =mysql_fetch_array ($result)) {$iid = $datauser [id]; $html = file_get_contents ("/moban.php?id=". $iid. "); $sql = "SELECT * from 2carsell where id= $iid"; $data =mysql_fetch_array (mysql_query ($sql)); $path =date ("Ym", $data [putdate]); $testdir = "html/". $path; if (file_exists ($testdir)): Else:mkdir ($testdir, 0777); echo "Catalog". $testdir. " Create success! <br> "; endif $filename = "html/$path/sell_$iid.html"; Use write mode to open $filename if (! $handle = fopen ($filename, ' W ')) {print "Cannot open file $filename"; exit;} if (is_writable ($filename)) {//writes $html to our open file. if (!fwrite ($handle, $html)) {print "Cannot write to file $filename"; exit;} Print "File $filename updated successfully! \n\r "; Fclose ($handle); } else {print "File $filename not writable";}?> <? }?>

<? Echo $datastat. " "?>

<? $AA = $aa +1; if ($aa > $bb) {echo ' Congratulations, all pages generated! '; echo "";} else{echo "";}? >

After reading the example, we go on to analyze and analyze

In general, there are two ways to use PHP to output HTML pages: The following are the articles that refer to prawns

The first: use templates. PHP is now a template can be said to be many, there are powerful smarty, there are easy-to-use smarttemplate and so on. Each of these templates has a function to get the output content. The way we generate static pages is to take advantage of this function. The advantage of this method is that the code is clear and readable.

Here I use Smarty as an example to show how to generate static pages:

<?phprequire ("smarty/smarty.class.php"); $t = new Smarty; $t->assign ("title", "Hello world!"); $content = $t->fetch ("templates/index.htm");//The Fetch () here is the function that gets the output, and now the $content variable inside is the content to be displayed $fp = fopen (" Archives/2005/05/19/0001.html "," w "); Fwrite ($fp, $content); fclose ($FP);? >

The second method: Use the functions of the OB series. The function used here is mainly Ob_start (), Ob_end_flush (), Ob_get_content (), where Ob_start () is the meaning of opening the browser buffer, when buffering is turned on, all non-file header information from the PHP program will not be sent , save it in the internal buffer until you use Ob_end_flush (). The most important function here is ob_get_contents (), the function of which is to get the contents of the buffer, equivalent to the fetch () above, the same reason.

<?phpob_start (); echo "Hello world!"; $content = Ob_get_contents ();//Get all the contents of the PHP page output $fp = fopen ("archives/2005/05/19/0001.html", "w"); Fwrite ($FP, $content) ; fclose ($fp);? >

The 2nd method I've chosen is the function of the OB series.

When I first started looking at this, I was a little bit confused, and then I realized that OB was output buffering.

When you're ready to output, all the data is stored in the OB. after the server resolves PHP, all the HTML code to be output to the client is stored in the OB if we want to output an HTML static page just write the cache out to an HTML page

So the principle is actually very simple

Here's a couple of functions. Because I'm a beginner of PHP's many functions I don't know, so here's a little bit of hope to help you.

Ob_start (): Start capture cache that's where you open your browser's cache from here.

Ob_end_flush (): Close browser cache

Ob_get_content (): Read Cache contents

fopen ("File path", "open Mode") open a file There are several main modes of opening the function, which are described below:

The "R" read-only mode opens, pointing the file pointer to the file header.

The "r+" read-write mode opens, pointing the file pointer to the file header.

The "W" Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.

The "w+" read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.

Fwrite ("File name", "Write content") write to File

Fclose () Close file

Because I want to convert the HTML file very many may have hundreds of, so here can not statically specify the path of the fopen, you may set a path variable inside can save the user ID and other information to facilitate the HTML file name below is a simple example of my last PHP reading XML data

<?phpob_start ()///Open Browser cache//below is read XML data $parser = Xml_parser_create (); Create a parser editor Xml_set_element_handler ($parser, "startelement", "endElement");//Set the corresponding function when the tag is triggered Here are startelement and Endelenmentxml_set_character_data_handler ($parser, "Characterdata"), and/or set the corresponding function $xml_file= when reading the data. 1.xml ";//Specifies the XML file to be read, which can be url$filehandler = fopen ($xml _file," R ");//Open File while ($data = Fread ($filehandler, 4096)) {Xml_ Parse ($parser, $data, feof ($filehandler));} Remove 4,096 bytes at a time to process fclose ($filehandler); Xml_parser_free ($parser);//Close and Release parser parser $name =false; $position =false; function startelement ($parser _instance, $element _name, $attrs)//start tag Event {global $name, $position; if ($element _name= = "Name") {$name =true; $position =false;echo "Name:";} if ($element _name== "POSITION") {$name =false; $position =true;echo "Position:";}} function Characterdata ($parser _instance, $xml _data)//functions {Global $name when reading data, $position, if ($position) echo $xml _data. "
"If ($name) echo $xml _data."
";} function EndElement ($parser _instance, $element _name)//end tag Event {global $name, $position; $name =false; $position = false;} The XML data has been read $htmlname = $id. ". HTML ";//$id can define the id$htmlpath=" archives/"on behalf of the user. $htmlname; Set the path variable $content = ob_get_contents ();//Get all the contents of the PHP page output $fp = fopen ($htmlpath, "w"); Fwrite ($fp, $content); fclose ($FP);? >

http://www.bkjia.com/PHPjc/1073133.html www.bkjia.com true http://www.bkjia.com/PHPjc/1073133.html techarticle HTML static pages are generated using PHP implementations, and PHP static pages generate HTML static pages from PHP and store them in a directory created with the name of the year and month. Read all data batch generation, all raw ...

  • 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.