PHP generation static page method and implementation code detailed version _php tutorial

Source: Internet
Author: User
Tags readfile
The main use in PHP is to use Fread () and Fwirte (). When a static page is generated, it involves a modification. A regular matching method can be used here to replace the changed parts of the template. However, this method is too cumbersome, the recommended method is to directly the original generated template cut off, re-generated, hehe, the real solve everything.
It is also important to note that this method of generating static pages is typically used for pages that do not change very frequently, such as the final page of information. For list pages, it is advisable if the information is not updated very frequently. Now popular online many can generate static pages of the blog or forum program, are manually click the background "Generate HTML page" button to "semi-automatic" generation of HTML. For some of the most informative portals, it won't work. Because static pages are called "static" because they are not automatically changed. If the list of information is updated 100 times a day, the static list page is regenerated 100 times. If I had 10 of these columns, it would be enough to vomit blood.
Well, gossip less, now take a look at the actual program demo:
First: An OB function is used to implement, the code is relatively simple, relatively high efficiency.
Copy CodeThe 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 static pages, dynamic read that part is to be retained, after inserting the data into the database, the URL is passed to the ReadFile function, and then read into the cache, fwrite can generate static pages, this is Camel camel most appreciate a practice. The least number of lines of code, the most efficient. http://tools.jb51.net/is a bare page, that is, pure content, no head, tail, menu. In order to be more free to customize their own template myfile.html. If you just want to generate static pages, this basically satisfies the requirement.
Second: normal generation of static HTML pages.
This is done in a step-by-step manner, fread in the page, and then Str_replace replace
The first is to create the final content page:
PHP code
Copy CodeThe 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 file pointer, create file
/* Check if the file is created and writable */
if (!is_writable ($filename))
{
Die ("File:". $filename. " Not writable, please check its properties and try again! ");
}
if (!fwrite ($handle, $content))
{//write information to file
Die ("Generate file". $filename. " Failed! ");
}
Fclose ($handle); Close pointer
Die ("Create file". $filename. " Success! ");
?>

This step is relatively straightforward. Just a simple variable substitution. If you want to generate a static list page, the principle is the same, using a program to generate a list of articles, as a large variable, replace the template in the variable, the list of page pages. Of course, if the information is updated, the list page will be regenerated.
PHP code
Copy CodeThe 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);
Build list Start
$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);//Generate List End
Echo $content;
$filename = "test/test.html";
$handle = fopen ($filename, "w");
Open file pointer, create file
/* Check if the file is created and writable */
if (!is_writable ($filename))
{
Die ("File:". $filename. " Not writable, please check its properties and try again! ");
}
if (!fwrite ($handle, $content))
{//write information to file
Die ("Generate file". $filename. " Failed! ");
}
Fclose ($handle); Close pointer
Die ("Create file". $filename. " Success! ");
?>

About page flipping:
If we specify paging, 20 articles per page. A sub-channel list of the article through the database query for 45, then, we first through the query to obtain the following parameters: 1, the total number of pages, 2, per page. The second step, for ($i = 0; $i < allpages; $i + +), page element acquisition, analysis, article generation, are executed in this loop. The difference is that die ("Create file". $filename. " Success! This sentence is removed and placed on the display after the loop, because the statement aborts the execution of the program.
Cases:
PHP code
Copy CodeThe 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); If the file already exists, delete the
}
$handle = fopen ($indexpath, "w"); Open file pointer, create file
/* Check if the file is created and writable */
if (!is_writable ($indexpath))
{
echo "File:". $indexpath. " Not writable, please check its properties and try again! "; Modify to Echo
}
if (!fwrite ($handle, $content))
{//write information to file
echo "Generate file". $indexpath. " Failed! "; Modify to Echo
}
Fclose ($handle); Close pointer
}
Fclose ($FP);
Die ("Raw ingredient page file complete, such as build incomplete, please check file permissions system after rebuild!") ");
?>

Third:smarty templates generate static pages
Smarty itself has a fetch function that is somewhat similar to fread () that can be used to generate static pages.
This example looks familiar to us, yes, the example of the FETCH function in the Smarty Handbook is always classic than the official example!
PHP code
Copy CodeThe code is as follows:
Include ("Smarty.class.php");
$smarty = new Smarty;
$smarty->caching = true;
Only do DB calls 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 place is critical//do something with $output here
Echo $output; HoHo See the results of the output, then what? fwrite, we get the results we want.
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content);
Fclose ($FP);
?>

PHP code
Copy CodeThe code is as follows:
Ob_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);
?>

http://www.bkjia.com/PHPjc/321292.html www.bkjia.com true http://www.bkjia.com/PHPjc/321292.html techarticle the main use in PHP is to use Fread () and Fwirte (). When a static page is generated, it involves a modification. Here you can use the regular matching method to replace the template in the change ...

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