This article is mainly to share with you the PHP implementation of bulk HTML static page, for not yet understand the template engine, or do not touch the framework. And I follow the principle of lazy and productivity, want to reduce repetitive work, avoid the time of unnecessary waste of classmates.
Principle: The use of str_replace substitution function in PHP,
First I need a database, I call him newsdata. and a table: (news) Specific as:
To make a point, the content of the news and the type of HTML path mentioned later is set to text, the memo is displayed in Mysql-front, does not affect the call, to view the content click the following will pop up a six-editor to see the content
Then you can create a connect.php to connect to the database, the advantage is that the direct reference (require_once) is ok later, or you can write directly in the PHP file
Then is to build the template file (model.html), because we only need to call the data, change the content is OK, the other like what navigation ah, footer Ah, the left-right background assorted are unchanged. I was the simplest to achieve, (to the enemy, lazy!) To understand, of course, can expand Ah, write a and I write a variety of cool-handed templates [manual funny], where to replace, curly braces around the opening of the whole.
<! DOCTYPE html>
By the way simply write a release form (modelform.html), submit to the immediate to say, the main add.php file
<! DOCTYPE html>
The next point is the add.php.
<?phpheader ("Content-type:text/html;charset=utf-8");//reference Connection database file require_once ("connect.php");//Get form data $title=$ _post["News_title"]; $content =$_post["news_contents"];//build a txt, the value is increased, used as the name $countfile= "Count.txt";//The file does not exist create if (! File_exists ($countFile)) {fopen ($countFile, "WB");} $handle =fopen ($countFile, "RB"), $num =fgets ($handle, 20);//Increase 1$num= $num +1;fclose ($handle) each time;//Update $num$handle=fopen ($countFile, "WB"); Fwrite ($handle, $num); fclose ($handle);//Get an HTML path, customize $extend= ". html"; $path = "News". $num. $ extend;//Insert Data $sql= "Insert News (News_title,news_contents,news_path) VALUES ('". $title. "', '". $content. "', '". $path. "');"; $conn->query ($sql);/**---start replacing---**///open HTML template $handle=fopen ("model.html", "RB");//Read template content $str=fread ($handle, FileSize ("model.html"));//Replace Str_replace ("replaced", "replace", "where to replace")//Why Replace in $STR? Because the template content that we read above, must be in the template to change the $STR=STR_ Replace ("{news_title}", $title, $str), $str =str_replace ("{news_contents}", $content, $str); fclose ($handle);// Write the contents of the replacement into the generated HTML file $handle=fopen ($path, "WB"); Fwrite ($handle, $STR); Fclose ($hanDLE);
Ok!
Finally thanks to "Yeqihong", referring to his article PHP three ways to generate static HTML pages. Good night!
"In case I don ' t see you,
Good Afternoon,good evening,and good night!—— "the World of Chu Gate"
PHP Simple implementation of batch generation HTML static page
Hello, I'm yangrl.
The original Ha up a lump (silly), wrote the page, copy 10 copies, next to the text, Imp,url and other content, made 10 pages (similar to the News page Ah, product details page, etc.) in fact, the overall framework has not changed, is the content has changed.
"Are you willing to be a Shar pei that can only repeat a boring job?" ”
Does not understand the template engine, or does not touch the framework. And I follow the principle of lazy and productivity, want to reduce repetitive work, avoid the time of unnecessary waste of classmates.
Principle: The use of str_replace substitution function in PHP,
First I need a database, I call him newsdata. and a table: (news) Specific as:
To make a point, the content of the news and the type of HTML path mentioned later is set to text, the memo is displayed in Mysql-front, does not affect the call, to view the content click the following will pop up a six-editor to see the content
Then you can create a connect.php to connect to the database, the advantage is that the direct reference (require_once) is ok later, or you can write directly in the PHP file
Then is to build the template file (model.html), because we only need to call the data, change the content is OK, the other like what navigation ah, footer Ah, the left-right background assorted are unchanged. I was the simplest to achieve, (to the enemy, lazy!) To understand, of course, can expand Ah, write a and I write a variety of cool-handed templates [manual funny], where to replace, curly braces around the opening of the whole.
<! DOCTYPE html>
By the way simply write a release form (modelform.html), submit to the immediate to say, the main add.php file
<! DOCTYPE html>
The next point is the add.php.
<?phpheader ("Content-type:text/html;charset=utf-8");//reference Connection database file require_once ("connect.php");//Get form data $title=$ _post["News_title"]; $content =$_post["news_contents"];//build a txt, the value is increased, used as the name $countfile= "Count.txt";//The file does not exist create if (! File_exists ($countFile)) {fopen ($countFile, "WB");} $handle =fopen ($countFile, "RB"), $num =fgets ($handle, 20);//Increase 1$num= $num +1;fclose ($handle) each time;//Update $num$handle=fopen ($countFile, "WB"); Fwrite ($handle, $num); fclose ($handle);//Get an HTML path, customize $extend= ". html"; $path = "News". $num. $ extend;//Insert Data $sql= "Insert News (News_title,news_contents,news_path) VALUES ('". $title. "', '". $content. "', '". $path. "');"; $conn->query ($sql);/**---start replacing---**///open HTML template $handle=fopen ("model.html", "RB");//Read template content $str=fread ($handle, FileSize ("model.html"));//Replace Str_replace ("replaced", "replace", "where to replace")//Why Replace in $STR? Because the template content that we read above, must be in the template to change the $STR=STR_ Replace ("{news_title}", $title, $str), $str =str_replace ("{news_contents}", $content, $str); fclose ($handle);// Write the contents of the replacement into the generated HTML file $handle=fopen ($path, "WB"); Fwrite ($handle, $STR); Fclose ($hanDLE);
Ok!