Php code used to generate a static HTML document when adding an article

Source: Internet
Author: User

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.

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.