The technical principle of generating HTML in PHP. 1) create a database in MYSQL and name it "database" (customizable). create a new table and name it "news" (because it is a news release, just give it a memorable name, can be customized), 1) create a database in MYSQL, name it database (customizable), create a table, and name it news (because it is a news release, take a memorable name, which can be customized) and create the following field names:
Id (auto increment, which is the key; type: INT)
Title (as the name suggests, News title, type can be TEXT)
Content (news content, type can be TEXT)
Path (HTML file path, type can be TEXT)
2) create conn. php
This is the php file for connecting to the database. you can separate the statement for connecting data into this file. you can directly reference this file in the future for multiple files that need to connect to the database.
3) design the add. form for adding news. The simple source code is as follows:
4) Create an HTML template named model.htm, which can be in the same directory as add. php.
Sample Source code:
Title of this news: {title}
Content of this news: {content}
{} The content in the braces is the content to be replaced. the whole static template can be designed according to your own ideas, but the content to be replaced in {} must be included, {title },{ content}; click here ~ Simply put, after designing a good-looking news template, place the tags such as {title} and {content} to the desired place.
5) add. php source code
Require_once ("conn. php"); // reference conn. php to connect to the database
$ Title = $ _ POST ["title"];
$ Content = $ _ POST ["content"]; // Obtain the form variable
// Create a text document with the following values automatically counted
$ Countfile = "count.txt ";
If (! File_exists ($ countfile ))
{
Fopen ($ countfile, "w"); // If this file does not exist,
}
$ Fp = fopen ($ countfile, "r ");
$ Num = fgets ($ fp, 20 );
$ Num = $ num + 1; // The value is automatically added each time.
Fclose ($ fp );
$ Fp = fopen ($ countfile, "w ");
Fwrite ($ fp, $ num); // update its value
Fclose ($ fp );
// Obtain the HTML path by using the value automatically counted above $ path
$Houzuiapps.html ";
$ Path = $ num. $ houzui;
// The result is automatic growth, such as 1.html, 2.html, 3.html .......... 1 is automatically added when a news item is added.
// Use the following SQL statement to add data to the table news
$ SQL = "insert into news (title, content, path) values ('". $ title. "','". $ content. "','". $ path. "')";
$ Query = mysql_query ($ SQL );
// Replace the data obtained from the form with the {title} and {content} tags in the template.
$Fpw.fopen(“model.htm "," r ") // read-only open template
$Str=fread($fp,filesize(“mode.htm "); // read the content in the template.
$ Str = str_replace ("{title}", $ title, $ str );
$ Str = str_replace ("{content}", $ content, $ str); // replace content
Fclose ($ fp );
$ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
Fwrite ($ handle, $ str); // write the replaced content into the generated HTML file.
Fclose ($ handle );
// Close the work:
Echo "view the news just added ";
OK. the source code of the sample code for generating HTML is here. The key is to use the replacement method.
$ Str = str_replace ("{replaced content}", $ replaced content, $ str );
Therefore, to sum up the above practices: first design a News template, put the content to be replaced in the corresponding position of {} in the template, and then design the form, the final form handler replaces the corresponding content in the template with the variables obtained from the form, so that different HTML is generated each time; if the HTML content needs to be modified is the same, after obtaining the modified form content, update the database with the update statement, and then replace the content in the template. if you delete the modification, delete the content in the table first, and then use unlink ($ path) to delete the HTML physical file.
Http://www.bkjia.com/PHPjc/371664.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371664.htmlTechArticle1) create a database in MYSQL, name it database (customizable), create a new table, name it news (because it is news, take a good note of the name, can be customized ),...