PHP to generate HTML ideas

Source: Internet
Author: User
Tags html sample connect mysql php source code mysql database
Many of the former web site's news release system has adopted dynamic server technology to generate static HTML practices, the benefits are: one can reduce the burden on its servers, the second is because of the generation of HTML static pages, so its Web site by search engines to search the probability of a greater number.

The author's website has used PHP this dynamic technology to build news release system, its principle is to use PHP to generate HTML static page technology, the related platform is Windows XP Sp2+php4.32+mysql, therefore, here, want to simply talk about this approach. This article is suitable for PHP+MYSQL database operations, SQL statements and Web design a little bit of a friend, if you are a friend from the beginning, then please lay a good foundation! You don't have to look down here. If you all meet the above conditions, then congratulations, please continue to look down. However, you need to do the following to prepare yourself before you start building a concrete action.

One, have the function of debugging PHP locally

Under the Windows XP operating system, I suggest that you can download a Php+mysql+aphche server suite, such as the Huajun Software Park, and go there and search for it. After downloading the default installation can, so you have a local test PHP function, save a lot of manual configuration trouble, how, simple, OK, this is just the first step.

Second, the concept of the news release system features

The first page of the news is often updated through the background, the background of the update is simply by adding, editing, delete data and other basic functions realized. Here, you can use web design software to build your desired background interface, the realization of the function of course is to use PHP. This step suggests that you first think about the features that the news release system needs. Here, how to add, edit, delete data in PHP no longer repeats, because the focus is on how to generate static technology on this basis.

Third, PHP to generate the technical principles of HTML.

Ha ha. The cost of saying so much, finally arrived at the place to speak. In fact, this principle is not complex, in general, it should be a substitution data syntax in PHP application. OK, let's talk about a simple example, step-by-step analysis! Believe that smart you can see the slightly, carefully look at each step can be, here, just guide everyone how to do it, concrete can practice it!

(1) Create a new database in MySQL, name it (customizable), create a new table, name News (because it's a press release, take a good name, you can customize it), and then create the names of these fields:
ID (auto increment, which is key, type: INT)
Title (as the name suggests, news headlines, type TEXT)
Content (News contents, type is preferable TEXT)
Path (HTML file path, type preferred TEXT)

(2) Establishment of conn.php
This is connected to the database of PHP files, you can connect the data to the statement alone in this file, the next several need to connect the database file directly refer to the file.

(3) The design adds the news the form add.form the simple source code as follows:

The following is a snippet of code that replaces "<>" with a:

"Form method=" POST "action=" add.php ">//Submit to add.php
News title: <input type= "text" name= "title" Size= "><br>"
News content: <textarea name= "Content" cols= "ten" rows= "></textarea><br>"
"Input type=" submit "Name=" submitted "
"/form"

(4) Create an HTML template, save as model.htm, and add.php can be in the same directory.
Sample source code:

The following is a snippet of code that replaces "<>" with a:

The html>
The body>
Title of this news: {title}
Content of this news: {content}
The/body>
The/html>
{} The contents of the curly braces are the contents to be replaced. The entire static template design can be based on their own ideas, but {} replaced content must be included, such as the above {title},{content}; Click to simply say, design a very good news template, to be replaced by the Title},{content} and so on to put the mark where you need it to sprinkle.

(5) Detailed add.php source code

The following is a snippet of code that replaces "<>" with a:

the? php
Require_once ("conn.php");//reference conn.php, connection database
title=_post["title"];
content=_post["Content"]; Get form variables

The following is a document that automatically counts the values
Countfile= "Count.txt";
if (!file_exists (countfile))
{
fopen (Countfile, "w"); If this file does not exist, it automatically creates a
}
Fp=fopen (Countfile, "R");
Num=fgets (fp,20);
num=num+1; Each time its value automatically adds a
Fclose (FP);
Fp=fopen (Countfile, "w");
Fwrite (Fp,num); Update its value
Fclose (FP);

Use the automatically counted values above to get the path to HTML
houzui= ". html";
Path=num.houzui;
This form of path is automatically increased, such as 1.html,2.html,3.html ... add a piece of news and automatically adds 1.

The following add data to table news with SQL statements

The following is a snippet of code that replaces "<>" with a:
Sql= "INSERT into News (Title,content,path) VALUES ('". Title. "', '". Content. "', '". path. ")";
Query=mysql_query (SQL);

The key here is to replace the {title},{content} tag in the template with the data obtained from the form

The following is a snippet of code that replaces "<>" with a:
Fp=fopen ("model.htm", "R")//read-only open template
Str=fread (Fp,filesize ("mode.htm"))//Read the contents of the template
Str=str_replace ("{title}", Title,str);
Str=str_replace ("{content}", content,str);/replace content
Fclose (FP);

Handle=fopen (Path, "w"); Write way to open a news path
Fwrite (HANDLE,STR); Write what you just replaced into the generated HTML file
Fclose (handle);

Finishing work:

The following is a snippet of code that replaces "<>" with a:
echo "<a Href=path target=_blank> View just added news </a>";

OK, the whole generation of HTML sample source code is here, the key is to use the replacement method.
Str=str_replace ("{Replaced content}", replaced content, str);

So, to sum up the above practice: first design a news template, place what needs to be replaced with {} into the appropriate location in the template, then design the form, then the final form handler, and replace the variable from the form with the corresponding content in the template, so that each time a different HTML is generated , if you need to modify the content of HTML is the same, get the modified form content, first update the database with the UPDATE statement, and then replace the contents of the template can be deleted, first delete the table to delete the content, and then use unlink (path) To delete the physical file of HTML.



Related Article

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.