How PHP generates HTML ideas

Source: Internet
Author: User
Tags html sample php source code
This article mainly introduces PHP how to generate HTML ideas, interested in a friend's reference, I hope to be helpful to everyone.

At present, many Web sites in the news release system are using dynamic server technology to generate static HTML, the advantage is: one is to alleviate the burden of its server, and second, because of the generation of HTML static pages, so its Web site by the search engine to find a greater probability. The author's website used PHP as a dynamic technology to build a news release system, the principle is that the use of PHP to generate HTML static page technology, the related platform is Windows XP sp2+php4.32+mysql, so here, want to briefly talk about this approach to the idea. This article is suitable for PHP+MYSQL database operation, SQL statements and Web page design a little basic friends, if you are a friend from the beginning to learn, then please lay the foundation! You don't have to look down here. If you all meet the above conditions, then congratulations, please continue to look down. However, before you build your hands-on, you should do the following to prepare for your work.

First, with the function of local debugging PHP

Under the Windows XP operating system, the author suggests that you can download a Php+mysql+aphche server package on the Internet, such as Huajun Software Park, to go there to search for it can be downloaded to. After downloading the default installation, so you have the ability to test PHP locally, save a lot of manual configuration trouble, how, simple, OK, this is just the first step.

Second, the concept of the function of the press release system

The first page of the news is often updated through the background, the background of the update is nothing more than add, edit, delete data, such as the basic functions of the implementation. Here, you can use the web design software to build your desired background interface, the implementation of the function of course is to use PHP. This step suggests that you first think about the features that the news release system has to offer. Here, how to add, edit, delete data in PHP is no longer duplicated, because the focus is on how to build static technology on this basis.



Third, the technical principle that PHP generates HTML.

Ha ha. The fee said so much, finally to the place to speak. In fact, this principle is not complex, in general speaking, it should be a replacement data syntax in PHP application. OK, tell me a simple example, step by step analysis! Believe that smart you can see clearly, carefully watch every step can, here, just guide you how to do it, concrete can practice!

(1) Create a new database in MySQL, name it (customizable), create a new table, name the news (because it is a press release, take a friendly name, can be customized), and then create these field names:
ID (auto increment, this is the key, type: INT)
Title (as the name implies, news headlines, type preferable TEXT)
Content (news contents, type preferable TEXT)
Path (HTML file path, type preferable TEXT)

(2) Establishing conn.php
This is the database to connect the PHP file, you can put the connection data statements in this file, a number of subsequent files need to connect to the database directly refer to the file.

(3) Design the form to add news add.form simple source code is as follows:

<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= "Submission" >  </form>

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


{} The contents of the curly braces are the content to be replaced, the entire static template design can be based on their own ideas, but {} The contents of the replacement must be included, such as {title},{content} above; Click to put it simply, after designing a nice news template, replace the { Title},{content} and so on where necessary, you can scatter.

(5) Detailed add.php source code

 <?php require_once ("conn.php");//reference conn.php, connection database $title =$_post["title"]; $content =$_post["Content"];   Get form variables//The following establishes a text document, whose value is automatically counted $countfile = "Count.txt";   if (!file_exists ($countfile)) {fopen ($countfile, "w"), or//If this file does not exist, automatically establishes a} $fp =fopen ($countfile, "R");   $num =fgets ($FP, 20); $num = $num +1;   Each time its value is automatically added a fclose ($FP);   $FP =fopen ($countfile, "w"); Fwrite ($fp, $num);   Update its value fclose ($FP);   Take advantage of the above automatically counted value to get the path of HTML $path $houzui = ". html";   $path = $num. $houzui; The resulting path is automatically growing, such as 1.html,2.html,3.html ... add a news automatically plus 1//Following add data to table in SQL statement news $sql = "INSERT INTO News (Title,content,   Path) VALUES (' ". $title." ', ' ". $content." ', ' ". $path." ') "; $query =mysql_query ($sql); The key here is to replace the data obtained from the form with the {title},{content} tag in the template $fp =fopen ("model.htm", "R")//read-only open the 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 mode Open News path fwrite ($handle, $STR);Write the content you just replaced into the generated HTML file fclose ($handle); Finishing touches: echo "<a href= $path target=_blank> to view the news </a>" that you just added;?>


OK, the entire generated HTML sample source is here, the key is to use the replacement method.
$str =str_replace ("{Replaced content}", the contents of the replacement, $STR);

Therefore, summarize the above approach: first design the news template, the need to replace the content with {} to the corresponding location in the template, and then design the form, and then the final form handler, the variables obtained from the form to replace the corresponding content in the template, so that each time will generate a different HTML If you need to modify the content of the HTML is the same, after obtaining the modified form content, first update the database with the UPDATE statement, and then replace the contents of the template, delete the table to delete the content, and then use unlink ($path) To delete the physical file of the HTML.

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.