As we all know, most website news or product information is static pages. The main benefits of doing so are: 1. Speed up access and avoid excessive Database Operations; 2. SEO optimization to facilitate search engine indexing.
This example describes how to generate static html in batches Based on the CMS Static Page solution.
Note: This program can only run in Windows DOS or Linux by executing the PHP Command.
In this example, there are four files: config. inc. php (configuration file), Db. class. php (Database PDO class), Model. class. php (PDO database operation class), index. php (Execution file)
Config. inc. php
Copy codeThe Code is as follows:
<? Php
Header ('content-Type: text/html; Charset = UTF-8 ');
Date_default_timezone_set ('prc ');
Define ('root _ path', dirname (_ FILE _); // ROOT directory
Define ('db _ DSN ', 'mysql: host = localhost; dbname = article'); // mysql PDO dsn
Define ('db _ user', 'root'); // database username
Define ('db _ pwd', '000000'); // Database Password (set as needed)
Function _ autoload ($ className ){
Require_once ROOT_PATH. '/includes/'. ucfirst ($ className). '. class. php ';
}
?>
Db. class. php
Copy codeThe Code is as follows:
<? Php
// Connect to the database
Class Db {
Static public function getDB (){
Try {
$ Pdo = new PDO (DB_DSN, DB_USER, DB_PWD );
$ Pdo-> setAttribute (PDO: ATTR_PERSISTENT, true); // set the database connection to persistent connection.
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // sets the throw Error
$ Pdo-> setAttribute (PDO: ATTR_ORACLE_NULLS, true); // you can change the string to NULL to the SQL NULL.
$ Pdo-> query ('set NAMES utf8'); // sets the database encoding.
} Catch (PDOException $ e ){
Exit ('database connection error, error message: '. $ e-> getMessage ());
}
Return $ pdo;
}
}
?>
Model. class. php
Copy codeThe Code is as follows:
<? Php
// Operate SQL
Class Model {
/**
* The SQL addition, deletion, and modification operations return the affected number of rows.
* @ Param string $ SQL
* @ Return int
*/
Public function aud ($ SQL ){
Try {
$ Pdo = Db: getDB ();
$ Row = $ pdo-> exec ($ SQL );
} Catch (PDOException $ e ){
Exit ($ e-> getMessage ());
}
Return $ row;
}
/**
* All data is returned, and the PDOStatement object is returned.
* @ Param string $ SQL
* @ Return PDOStatement
*/
Public function getAll ($ SQL ){
Try {
$ Pdo = Db: getDB ();
$ Result = $ pdo-> query ($ SQL );
Return $ result;
} Catch (PDOException $ e ){
Exit ($ e-> getMessage ());
}
}
}
?>
Index. php
Copy codeThe Code is as follows:
<? Php
Require_once './config. inc. php ';
$ M = new Model ();
$ Ids = $ m-> getAll ("SELECT id FROM article order by id ASC ");
Foreach ($ ids as $ rowIdArr ){
$ IdStr. = $ rowIdArr ['id']. ',';
}
$ IdStr = rtrim ($ idStr, ','); // The ID set of all articles
$ IdArr = explode (',', $ idStr); // split it into arrays.
// The following program cyclically generates a static page
Foreach ($ idArr as $ articleId ){
$ Re = $ m-> getAll ("SELECT id, title, date, author, source, content FROM article WHERE id = ". $ articleId); // $ re indicates the content of each article. Note: its type is PDOStatement.
$ Article = array (); // $ article is an array that stores the title, date, author, content, and source of each article.
Foreach ($ re as $ r ){
$ Article = array (
'Title' => $ r ['title'],
'Date' => $ r ['date'],
'Author' => $ r ['author'],
'Source' => $ r ['source'],
'Content' => $ r ['content']
);
}
$ ArticlePath = ROOT_PATH. '/article'; // $ articlePath is the directory where the static page is placed.
If (! Is_dir ($ articlePath) mkdir ($ articlePath, 0777); // check whether the directory exists. If it does not exist, create
$ FileName = ROOT_PATH. '/article/'. $ articleId. '.html '; // static file name generated by $ fileName. format: article id.html (primary key ID cannot conflict with each other)
$ ArticleTemPath = ROOT_PATH. '/templates/article.html'; // $ articleTemPath document template path
$ ArticleContent = file_get_contents ($ articleTemPath); // get the content in the template
// Replace the variables set in the template. For example, replace <{title}> in the template with the title read in the database, and assign the value to the variable $ articleContent.
$ ArticleContent = getArticle (array_keys ($ article), $ articleContent, $ article );
$ Resource = fopen ($ fileName, 'w ');
File_put_contents ($ fileName, $ articleContent); // write the HTML file
}
/**
* GetArticle ($ arr, $ content, $ article) replaces the template.
* @ Param array $ arr Replace the variable array
* @ Param string $ content template content
* @ Param array $ article an array of the content of each article. Format: array ('title' => xx, 'date' => xx, 'author' => xx, 'source' => xx, 'content' => xx );
*/
Function getArticle ($ arr, $ content, $ article ){
// Cyclic replacement
Foreach ($ arr as $ item ){
$ Content = str_replace ('<{'. $ item. '}>', $ article [$ item], $ content );
}
Return $ content;
}
?>
Running (Windows DOS is used as an example)
Completed:
You can generate more than 9000 html files in about 2 minutes.
The column from Lee. is reprinted to indicate the source !!!