Introduction to the prerequisites and principles for generating HTML in PHP. Many websites have begun to use the author's website to use the dynamic PHP technology to build a news publishing system. The principle is to use PHP to generate HTML static pages, many websites are using related platforms.
My website used the dynamic PHP technology to build a news publishing system. The principle is to use the PHP technology to generate HTML static pages, the related platform is Windows XP Sp2 + php4.32 + mysql. Therefore, I would like to briefly discuss this approach.
This article is suitable for PHP + MYSQL database operations, SQL statements, and web design. if you are a friend from the beginning, please lay the foundation first! You don't need to read it down here. If you all meet the preceding conditions, congratulations, please proceed. However, before you build PHP to generate HTML, make the following preparations.
1. php html generation requires local PHP debugging.
In windows xp, I suggest you download a PHP + MYSQL + APHCHE server package from the Internet, such as the Huajun Software Park. you can search for it and download it. After the download is complete, you can install it by default. in this way, you can test the PHP function locally, saving the trouble of manual configuration. how can this problem be solved? easy, OK, this is only the first step.
2. generating HTML in PHP also involves the functions of the news publishing system.
Homepage news publishing is often updated through the background, and background updates are only implemented by basic functions such as adding, editing, and deleting data. Here, you can use web design software to build the background interface you want. the implementation of its functions is of course PHP. In this step, we recommend that you first consider the functions required by the news publishing system. Here, how to use PHP to add, edit, and delete data is no longer repeated, because the focus is on generating static technologies based on this.
III. technical principles of generating HTML in PHP.
Haha. After talking so much, I finally got to the point where I should talk about it. In fact, this principle is not complicated. In general, it should be an application in PHP that replaces the data syntax. OK. Let's take a simple example and analyze it step by step! I believe that you can understand what you are smart. just look at each step carefully. here, we will only guide you on how to do it. you can practice it in detail!
(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:
News title:
- <Input type = "text" size = "20">
- <Br>
- News: <textarea
Cols = "10" rows = "25">
- </Textarea> <br>
- <Input type = "submit"
- </Form>
- <Form method = "post"
Action = "add. php">
// Submit to add. php
- News Title: <input type = "text"
Size = "20"> <br>
- News content: <textarea cols = "10"
Rows = "25"> </textarea> <br>
- <Input type = "submit"
- </Form>
(4) Create a php HTML template, which is model.htm, and add. php can be in the same directory.
Sample Source code:
- <Html>
- <Body>
- Title of this news: {title}
- Content of this news: {content}
- </Body>
- </Html>
{} 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 );
- <? Php
- Require_once ("conn. php ");
- // Reference conn. php to connect to the database
- $ Title = $ _ POST ["title"];
- $ Content = $ _ POST ["content"];
- // Obtain the form variable
- $ 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 );
- $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
- $ Query = mysql_query ($ SQL );
- // The following is the key to replace the data obtained from the form
Tag {title} and {content} in the template
- $Str=fread($fp,filesize(“mode.htm "));
- // Read the template content
- $ Str = str_replace ("{title}", $ title, $ str );
- $ Str = str_replace ("{content}", $ content, $ str );
- // Replace content
- Fclose ($ fp );
- $ Handle = fopen ($ path, "w ");
- // Open the news path in writing mode
- Fwrite ($ handle, $ str );
- // Write the replaced content into the generated HTML file
- Fclose ($ handle );
- $Fpw.fopen(“model.htm "," r ")
- // Read-only open template
- $Str=fread($fp,filesize(“mode.htm "));
- // Read the template content
- $ Str = str_replace ("{title}", $ title, $ str );
- $ Str = str_replace ("{content}", $ content, $ str );
- // Replace content
- Fclose ($ fp );
- Fwrite ($ handle, $ str );
- // Write the replaced content into the generated HTML file
- Fclose ($ handle );
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 php html generation practices: first design the news template, put the content to be replaced with {} in the corresponding position of 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.
Author's website once used the dynamic PHP technology to build a news publishing system. The principle is to use the PHP technology to generate HTML static pages...