How PHP generates HTML

Source: Internet
Author: User
Currently, many website news and publishing systems on the Internet use dynamic servers. Technology Generate static Html In this way, the benefits are: first, it can reduce the burden on its server, and second, it is because HTML static pages are generated, so its website is more likely to be searched by the search engine. My website used PHP This dynamic technology is used to build a news publishing system. The principle is to use PHP to generate HTML static pages. The related platform is Windows XP SP2 + php4.32 + MySQL. Therefore, here, I would like to briefly talk about this approach. This article Article Suitable for PHP + MySQL database operations, SQL statements, and Webpage Design If you are a friend from scratch, 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, you need to make the following preparations before you build it. Work .

1. 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. Design 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 repeat, 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) InMySQLCreate 1 inDatabaseName it "Database" (customizable), create a new table, and name it "news" (because it is a news release, just get a memorable name, which can be customized ), 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 a simple source for adding a news table add. FormCodeAs follows:
<Form method = "Post" Action = "Add. php"> // submit it to add. php.
News Title: <input type = "text" name = "title" size = "20"> <br>
News content: <textarea name = "content" Cols = "10" rows = "25"> </textarea> <br>
<Input type = "Submit" name = "Submit">
</Form>

Code copy box
<Form method = "Post" Action = "add. PHP "> // submit to add. PHP news title: <input type = "text" name = "title" size = "20"> <br> news content: <textarea name = "content" Cols = "10" rows = "25"> </textarea> <br> <input type = "Submit" name = "Submit"> </ form>
[Ctrl + A select all and copy]

(4) Create an HTML template named model.htm, which can be in the same directory as ADD. php.
ExampleSource 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
<? PHP
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 );

Code copy box
<? PHP require_once ("Conn. PHP "); // reference Conn. PHP, connect to the database $ title =$ _ post ["title"]; $ content =$ _ post ["content"]; // obtain the form variable // create a text document with the value of $ countfile = "count.txt"; if (! File_exists ($ countfile) {fopen ($ countfile, "W"); // if this file does not exist, a file is automatically created.} $ fp = fopen ($ countfile, "R"); $ num = fgets ($ FP, 20); $ num = $ num + 1; // The value of fclose ($ FP) is automatically added each time ); $ fp = fopen ($ countfile, "W"); fwrite ($ FP, $ num); // update its value fclose ($ FP );
[Ctrl + A select all and copy]

// 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 );

Code copy box
$Fpw.fopen(“model.htm "," R ") // read-only open template mongostr#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"); // write mode to open the news path fwrite ($ handle, $ Str); // write the replaced content into the generated HTML file fclose ($ handle );

[Ctrl + A select all and copy]
// Close the work:
Echo "<a href = $ PATH target = _ blank> View the news just added </a> ";

Code copy box
Echo "<a href = $ PATH target = _ blank> View the news just added </a> ";

[Ctrl + A select all and copy]
OK. The source code of the sample code for generating HTML is here. The key is to replace itMethod.
$ STR = str_replace ("{replaced content}", $ replaced content, $ Str );

Therefore, to sum up the above practices: first design the news template, put the content to be replaced in the corresponding position of the template {}, then design the form, and then process the final form.ProgramReplace the variables obtained from the form with the corresponding content in the template, so that different HTML will be generated each time. If you need to modify the HTML content, after obtaining the modified form content, use the update statement to update the database, and then replace the content in the template. If you delete the content in the delete table, use unlink ($ PATH) to delete HTML physical files.

Others:

Http://www.codesky.net/article/doc/201004/2010041701506.htm

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.