PHP generates static Article HTML in batches, but the standard is to generate an HTML article when adding the article and regenerate the HTML article when editing it, when you delete an article, you can also delete unnecessary HTML articles. In this case, batch generation is not enough. Next we will introduce how PHP generates static HTML files when adding articles.
Simply add an article form and you will not write it here. The following source code is the source code of the program that accepts the value passed in the form. You can test it first...
Copy codeThe Code is as follows: <? Php
Ob_start ();
Require_once ("../inc/conn. php ");
$ Typ = $ _ POST ["typ"];
$ Title = $ _ POST ["title"];
$ Content = $ _ POST ["d_content"];
$ Author = $ _ POST ["author"];
$ Source = $ _ POST ["source"];
$ Mobanpath = "../moban/moban.html ";
If (file_exists ($ mobanpath ))
{
$ Fp = fopen ($ mobanpath, "r ");
$ Str = fread ($ fp, filesize ($ mobanpath ));
$ Str = str_replace ("-title-", $ title, $ str );
$ Str = str_replace ("-time-", date ("Y-m-d H: I: s"), $ str );
$ Str = str_replace ("-content-", $ content, $ str );
$ Str = str_replace ("-author-", $ author, $ str );
$ Str = str_replace ("-source-", $ source, $ str );
$ Foldername = date ("Y-m-d ");
$ Folderpath = ".../newslist/". $ foldername;
If (! File_exists ($ folderpath ))
{
Mkdir ($ folderpath );
}
$ Filename = date ("H-I-s"). ". html ";
$ Filepath = "$ folderpath/$ filename ";
If (! File_exists ($ filepath ))
{
$ Fp = fopen ($ filepath, "w ");
Fputs ($ fp, $ str );
Fclose ($ fp );
}
$ Filepath = $ foldername. "/". $ filename;
$ SQL = "insert into newscontent (newstypeid, newstitle, newspath, newssource, newstime) values ($ typ, '$ title',' $ filepath', '$ source ', '". date ("Y-m-d H: I: s "). "')";
Mysql_query ($ SQL );
Header ("location: add. php ");
}
?>
Ob_start () indicates that the session is enabled. The write-not write relationship is not very large. It is added here according to the PHP standard writing method.
The second sentence is the file containing the link to the database.
The following $ content = $ _ POST ["content"]; is the content of the form accepted. Accept a few items.
$ Mobanpath = "../moban/moban.html"; this is the template path.
If (file_exists ($ mobanpath): checks whether the template file exists. if yes, perform the following template tag replacement operation.
The next step is to use str_replace to replace the template tag. At the same time, create an HTML file and add it to the database using SQL statements. Then return to add. where php adds an article tag, the HTML rules generated here can be added by yourself, for example, generated by time or by article ID.