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), and then create these field names
(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, take a memorable name, can be customized), and then create these 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:
XML/HTML code
(4) Create an HTML template named model.htm, which can be in the same directory as add. php.
Sample Source code:
XML/HTML 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
PHP 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 );
- // The following is the key to replacing the {title} in the template with the data obtained from the form, and the {content} is marked with dynamic fp1_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.