PHP's idea of generating HTML

Source: Internet
Author: User

Preface:

At present, the news and publishing systems of many websites on the Internet use dynamic server technology to generate static HTML. The advantages of this method are: first, it can reduce the burden on its servers, second, because HTML static pages are generated, their websites are more likely to be searched by search engines. 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, you must make the following preparations before you build the SDK.

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 repeated, because the focus is on generating static technologies based on this.

There are two pages in this news. Currently, there are two pages in page 1st.


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:
<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.
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
<? 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 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.

There are two pages in this news, currently on page 1, 2nd

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.