In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the prerequisite and principle for PHP to generate HTML? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Many websites have begun to use PHP as the basic language for website development, because its powerful features have attracted the attention of programmers. Below we will introduce in detail the relevant principles of generating HTML from PHP and so on.
The author's website has used the dynamic technology of PHP to build a news release system, and its principle is to apply the technology of PHP to generate HTML static pages, and the related platform is Windows XP Sp2+php4.32+mysql. Therefore, here, I would like to briefly talk about the idea of this practice.
This article is suitable for friends who have some basic knowledge of PHP+MYSQL database operations, SQL statements and web design. If you are a friend who learns from scratch, please lay the foundation first. You don't have to look down here. If you all meet the above conditions, then congratulations, please read on. However, before you actually build PHP to generate HTML, you need to do the following preparation work.
1. PHP needs to have the function of debugging PHP locally to generate HTML
Under the WINDOWS XP operating system, the author suggests that you can download a PHP+MYSQL+APHCHE server suite from the Internet, such as Huajun Software Park, which can be downloaded by searching there. After downloading, you can install it by default, so you can test PHP locally, saving you a lot of trouble of manual configuration. How about, simple, OK, this is just a * step.
Second, PHP generates HTML and conceives the functions of the press release system.
The news release of the home page is often updated through the background, and the background update is nothing more than by adding, editing, deleting data and other basic functions. Here, you can use web design software to build the background interface you want, and its function is realized by using PHP, of course. In this step, it is recommended that you first think about the functions of the press release system. Here, how to add, edit, and delete data with PHP will not be repeated, because the focus is on how to generate static technology on this basis.
Third, the technical principle of generating HTML by PHP.
Ha ha. After talking so much, I finally got to the place where I should talk. In fact, this principle is not complicated, generally speaking, it should be an application of alternative data syntax in PHP. OK, tell me a simple example and analyze it step by step! Believe that smart you can see it, carefully watch every step, here, just guide us how to do it, specific can be put into practice!
(1) create a new database in MYSQL, name it database (customizable), create a new table and name it news (because it is a press release, you can choose a name that is easy to remember, you can customize it), 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 content, type preferable TEXT)
Path (HTML file path, TEXT is preferred)
(2) establish conn.php
This is the PHP file that connects to the database. You can put the statements to connect to the database separately in this file, and then several files that need to connect to the database can refer to this file directly.
(3) the simple source code of add.form for designing a form to add news is as follows:
News headlines:
< input type="text" size="20"> < br>News content:
< textarea cols="10" rows="25"> < /textarea> < br> < input type="submit" < /form> < form method="post" action="add.php">/ / submit to add.php
News headlines:
< input type="text" size="20"> < br>News content:
< textarea cols="10" rows="25"> < /textarea> < br> < input type="submit" < /form>(4) create a template for PHP to generate HTML, save it as model.htm, and add.php can be in the same directory.
Sample source code:
< html> < body>The title of this news: {title} the content of this news: {content}
< /body> < /html>The content in {} curly braces is the content to be replaced, and the whole static template can be designed according to your own ideas, but the content to be replaced in {} must be included, such as {title}, {content} above; simply put, after designing a good-looking news template, you can put the tags to be replaced, such as {title}, {content}, and so on, where you need them.
(5) detailed explanation of add.php source code
Require_once ("conn.php")
/ / reference conn.php to connect to the database
$title=$_POST ["title"]
$content=$_POST ["content"]
/ / get form variables
/ / create a text document below, whose values are counted automatically
$countfile= "count.txt"
If (! file_exists ($countfile))
{
Fopen ($countfile, "w"); /
/ if this file does not exist, one is automatically created
}
$fp=fopen ($countfile, "r")
$num=fgets ($fp,20)
$num=$num+1; / / each time its value is automatically increased by one
Fclose ($fp)
$fp=fopen ($countfile, "w")
Fwrite ($fp,$num); / / update its value
Fclose ($fp)
php
Require_once ("conn.php")
/ / reference conn.php to connect to the database
$title=$_POST ["title"]
$content=$_POST ["content"]
/ / get form variables
$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; / / each time its value is automatically increased by one
Fclose ($fp)
$fp=fopen ($countfile, "w")
Fwrite ($fp,$num); / / update its value
Fclose ($fp)
$houzui= ".html"
$path=$num.$houzui
/ / the path formed in this way grows automatically, such as 1.html
2.htmlMagne3.html. . Add a piece of news and automatically add 1.
/ / use the SQL statement to add data to the table news
$query=mysql_query ($sql)
/ / the following is the key point to replace the data obtained from the form
{title}, {content} tags in 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")
/ / Open the news path by writing
Fwrite ($handle,$str)
/ / write the content you just replaced into the generated HTML file
Fclose ($handle)
$fp=fopen ("model.htm", "r")
/ / Open template read-only
$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)
Fwrite ($handle,$str)
/ / write the content you just replaced into the generated HTML file
Fclose ($handle)
OK, this is the end of the sample source code for generating HTML, and the key is to use a replacement method.
$str=str_replace ("{replaced content}", $replaced content, $str)
Therefore, summarize the above PHP HTML generation practice: first design the news template, put the content to be replaced in the appropriate location in the template with {}, then design the form, and then the * form handler, and replace the corresponding content in the template with the variables obtained from the form, so that a different HTML will be generated each time.
If you need to modify the content of HTML is the same, after obtaining the modified content of the form, first update the database with the update statement, and then replace the contents in the template; delete, first delete the contents of the delete table, and then use unlink ($path) to delete the physical file of HTML.
After reading the above, have you mastered the prerequisites and principles of generating HTML by PHP? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.