Prerequisites and principles for generating HTML in PHP _ PHP Tutorial

Source: Internet
Author: User
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:

 
 
  1. <Input type = "text" size = "20">
  2. <Br>
  3. News: <textarea
    Cols = "10" rows = "25">
  4. </Textarea> <br>
  5. <Input type = "submit"
  6. </Form>

 
 
  1. <Form method = "post"
    Action = "add. php">
    // Submit to add. php
  2. News Title: <input type = "text"
    Size = "20"> <br>
  3. News content: <textarea cols = "10"
    Rows = "25"> </textarea> <br>
  4. <Input type = "submit"
  5. </Form>

(4) Create a php HTML template, which is model.htm, and add. php can be in the same directory.
Sample Source code:

 
 
  1. <Html>
  2. <Body>
  3. Title of this news: {title}
  4. Content of this news: {content}
  5. </Body>
  6. </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

 
 
  1. Require_once ("conn. php ");
  2. // Reference conn. php to connect to the database
  3. $ Title = $ _ POST ["title"];
  4. $ Content = $ _ POST ["content"];
  5. // Obtain the form variable
  6. // Create a text document with the following values automatically counted
  7. $ Countfile = "count.txt ";
  8. If (! File_exists ($ countfile ))
  9. {
  10. Fopen ($ countfile, "w ");/
  11. /If this file does not exist,
  12. }
  13. $ Fp = fopen ($ countfile, "r ");
  14. $ Num = fgets ($ fp, 20 );
  15. $ Num = $ num + 1; // The value is automatically added each time.
  16. Fclose ($ fp );
  17. $ Fp = fopen ($ countfile, "w ");
  18. Fwrite ($ fp, $ num); // update its value
  19. Fclose ($ fp );
  20. <? Php
  21. Require_once ("conn. php ");
  22. // Reference conn. php to connect to the database
  23. $ Title = $ _ POST ["title"];
  24. $ Content = $ _ POST ["content"];
  25. // Obtain the form variable
  26. $ Countfile = "count.txt ";
  27. If (! File_exists ($ countfile ))
  28. {
  29. Fopen ($ countfile, "w ");
  30. // If this file does not exist,
  31. }
  32. $ Fp = fopen ($ countfile, "r ");
  33. $ Num = fgets ($ fp, 20 );
  34. $ Num = $ num + 1; // The value is automatically added each time.
  35. Fclose ($ fp );
  36. $ Fp = fopen ($ countfile, "w ");
  37. Fwrite ($ fp, $ num); // update its value
  38. Fclose ($ fp );
  39. $Houzuiapps.html ";
  40. $ Path = $ num. $ houzui;
  41. // The result is automatic growth, such as 1.html,
    2.html,3.html .......... 1 is automatically added when a news item is added.
  42. // Use the following SQL statement to add data to the table news
  43. $ Query = mysql_query ($ SQL );
  44. // The following is the key to replace the data obtained from the form
    Tag {title} and {content} in the template
  45. $Str=fread($fp,filesize(“mode.htm "));
  46. // Read the template content
  47. $ Str = str_replace ("{title}", $ title, $ str );
  48. $ Str = str_replace ("{content}", $ content, $ str );
  49. // Replace content
  50. Fclose ($ fp );
  51. $ Handle = fopen ($ path, "w ");
  52. // Open the news path in writing mode
  53. Fwrite ($ handle, $ str );
  54. // Write the replaced content into the generated HTML file
  55. Fclose ($ handle );
  56. $Fpw.fopen(“model.htm "," r ")
  57. // Read-only open template
  58. $Str=fread($fp,filesize(“mode.htm "));
  59. // Read the template content
  60. $ Str = str_replace ("{title}", $ title, $ str );
  61. $ Str = str_replace ("{content}", $ content, $ str );
  62. // Replace content
  63. Fclose ($ fp );
  64. Fwrite ($ handle, $ str );
  65. // Write the replaced content into the generated HTML file
  66. 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...

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.