The principle of generating static HTML documents using PHP
- 2020-05-26 07:59:37
- OfStack
Give the code:
<?php
// Introduction of database configuration files
include( dirname(dirname(__FILE__))."\include\config.php" );
/**
*
* Generate a single article in the database HTML file .
* @param Date $Date
* @param Time $Time
* @param String $Content
* @param String $Title
*/
function GenerateHTML($Date,$Time,$Content,$Title,$Name){
// Decompose the date and time variables into arrays
$GetDateRow = explode("-", $Date);
$GetTimeRow = explode(":",$Time);
// Get the name of the file. Such as: 20121028210632.html
$FileName = $GetDateRow[0].$GetDateRow[1].$GetDateRow[2].$GetTimeRow[0].$GetTimeRow[1].$GetTimeRow[2].".html";
// Open and read the template contents
$FP = fopen("tmp.html","r");
$Str = fread($FP,filesize("tmp.html"));
// Gets the replaced template content
$Str = str_replace("{Title}",$Title, $Str);
$Str = str_replace("{Content}", $Content, $Str);
$Str = str_replace("{Name}", $Name, $Str);
$Str = str_replace("{Date}", $Date,$Str);
$Str = str_replace("{Time}", $Time, $Str);
// Close the file to reduce the pressure on the server.
fclose($FP);
// Write to HTML file
$Handle = fopen($FileName,"w");
fwrite($Handle,$Str);
fclose($Handle);
// quiz 1 Under the
//echo "ok,done!";
}
// Operation of database
$querysql = "select * from article";
$queryset = mysql_query($querysql);
// Loop to generate HTML File.
while( $row = mysql_fetch_array($queryset) ){
GenerateHTML($row['date'],$row['time'],$row['content'],$row['title'],$row['name']);
}