PHP to generate static page and implementation code details version _php instance

Source: Internet
Author: User
Tags fread readfile
The main use of PHP is to use Fread () and Fwirte (). The static page generated after, will be involved in the problem of modification. Here you can use a regular matching method to replace the changed parts of the template. However, this method is too troublesome, the recommended method is directly to the original generated template cut off, rebuilt, hehe, the real it.
It is also important to note that this method of generating static pages is generally used for pages where changes are not frequent, such as the final page of information. For list pages, it is also desirable if information updates are not frequent. Now the online popular many can generate static page blog or forum program, are manually click the background "Generate HTML page" button to "semi-automatic" generation of HTML. And for some of the information is very large portal sites, it will not 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 will be regenerated 100 times. If I had 10 of these columns, it would be enough to spit blood.
Well, gossip less, now look at the actual program demo:
A: It is implemented using the OB function, the code is simpler and the efficiency is relatively high.
Copy Code code as follows:

<?php
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, the data into the database, the URL passed to the ReadFile function, and then read into the cache, fwrite can generate static page, this is the camel camel most appreciate a practice. The least number of lines of code, the most efficient. http://tools.jb51.net/is a bare page, which is simply content, without headers, tails, menus. In order to be more free to customize their own template myfile.html. If you just want to generate a static page, this basically satisfies the requirements.
Second: normal generation of static HTML pages.
This is done step-by-step, fread in the page, and then Str_replace replace
The first is to create the final content page:
PHP code
Copy Code code as follows:

<?php
$title = "http://siyizhu.com test template";
$file = "Twomax Inter test templet,<br>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 whether 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 simple. Just a simple variable to replace it. If you want to generate a static list page, the principle is the same, use the program to generate a list of articles, it as a large variable, replace the template in the variable, the page of the list is so. Of course, if there is information update, the page of the list will also be regenerated.
PHP code
Copy Code code as follows:

<?php
$title = "http://";
$file = "Twomax Inter test templet,<br>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. = ' <a href= '. $root. $result [' filename ']. ' target=_blank> '. $result [' title ']. ' </a><br> ';
}
$content. = Str_replace ("{articletable}", $list, $content);//Build list End
Echo $content;
$filename = "test/test.html";
$handle = fopen ($filename, "w");
Open file pointer, create file
/* Check whether 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 paging:
If we specify pagination, each page is 20 pieces. A channel list of the article through the database query for 45, then, first of all, we get the following parameters through the query: 1, the total number of pages, 2, per page. The second step, for ($i = 0; $i < allpages $i + +), page element acquisition, analysis, and article generation, are executed in this loop. The difference is that die ("Create file". $filename. " Success! This sentence is removed and put into the loop after the display, because the statement will abort the program execution.
Cases:
PHP code
Copy Code code as follows:

<?php
$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. = ' <a href= '. $root $result [' filename ']. ' target=_blank> '. $title. ' </a><br> ';
}
$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 whether 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 ("The Raw content page file completes, if the build is not complete, please check the file permission system to regenerate!" ");
?>

Third:smarty templates generate static pages
Smarty has a fetch function that is somewhat similar to the fread () that can be used to generate static pages.
This example, you must look familiar, yes, the Smarty manual on the FETCH function example, than the official example is always very classic!
PHP code
Copy Code code as follows:

<?php
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 the key//do something with $output here
Echo $output; HoHo See the results of output then? fwrite, we'll get the results we want.
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content);
Fclose ($FP);
?>

PHP code
Copy Code code as follows:

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

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.