Home > Article > Backend Development > Ideas on how PHP generates HTML
This article mainly introduces the idea of how PHP generates HTML. Interested friends can refer to it. I hope it will be helpful to everyone.
Currently, the news release systems of many websites on the Internet use dynamic server technology to generate static HTML. The benefits of this are: first, it can reduce the burden on their servers, and second, it generates HTML static pages. , so its website has a greater chance of being searched by search engines. The author's website once used PHP, a dynamic technology, to build a news release system. The principle is to apply PHP's technology to generate HTML static pages. The relevant platform is Windows XP Sp2 php4.32 mysql. Therefore, here, I want to briefly talk about it. Let’s look at the idea of this approach. 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 wants to learn from scratch, then please lay a solid foundation first! There is no need to look down here. If you meet the above conditions, congratulations, please read on. However, before actually building it, you need to make the following preparations.
1. With the function of local debugging of PHP
Under the WINDOWS XP operating system, the author recommends that you download a PHP MYSQL APHCHE server package from the Internet, such as Huajun Software Park, and search there It can be downloaded in one click. After downloading, you can install it by default. This way you will have the ability to test PHP locally, saving you a lot of trouble with manual configuration. How about, keep it simple, OK, this is just the first step.
2. Conceive the functions of the news release system
News releases on the homepage are often updated through the background. The updates in the background are nothing more than basic functions such as adding, editing, and deleting data. realized. Here, you can use web design software to build the backend interface you want. Of course, PHP is used to implement its functions. In this step, it is recommended that you first think about the functions that the news release system should have. Here, how to use PHP to add, edit, and delete data will not be repeated, because the focus is on how to generate static technology on this basis.
#3. The technical principle of PHP generating HTML.
Ha ha. Fei has said so much, and finally it’s time to talk. In fact, this principle is not complicated. Generally speaking, it should be an application of replacing data syntax in PHP. OK, let’s talk about a simple example and analyze it step by step! I believe you are smart and can understand it clearly. Just watch every step carefully. Here, I just guide you on how to do it. You can practice it in detail!
(1) Create a new database in MYSQL and name it database (can be customized). Create a new table and name it news (because it is a news release, just give it a name that is easy to remember. You can customize it. Definition), and then create these field names:
id (auto-increment, this is the key, type: INT)
title (as the name suggests, news title, the type can be TEXT)
content (news content, type TEXT is optional)
path (HTML file path, type is TEXT)
(2) Create conn.php
This is the PHP file to connect to the database. You can put the statement to connect the data separately. In this file, multiple files that need to connect to the database in the future can directly reference this file.
(3) Design the add.form form for adding news. The simple source code is as follows:
<form method=”post” action=”add.php”> //提交至 add.php 新闻标题:<input type=”text” name=”title” size=”20”><br> 新闻内容:<textarea name=”content” cols=”10” rows=”25”></textarea><br> <input type=”submit” name=”提交”> </form>
(4) Create an HTML template, save it as model.htm, and add.php. in the same directory.
Sample source code:
<html> <body> 此新闻的标题:{title} 此新闻的内容:{content} </body> </html>
{ } The content within the curly brackets is the content to be replaced. The design of the entire static template can be based on your own ideas, but the replaced content within { } must include Inside, such as {title}, {content}; Kaka~ Simply put, after designing a good-looking news template, put the tags to be replaced, such as {title}, {content}, etc. into the required Just place it.
(5) Detailed explanation of add.php source code
##
<?php require_once(“conn.php”); //引用conn.php,连接数据库 $title=$_POST[“title”]; $content=$_POST[“content”]; //获得表单变量 //以下建立一文本文档,其值自动计数 $countfile="count.txt"; if(!file_exists($countfile)) { fopen($countfile,"w"); //如果此文件不存在,则自动建立一个 } $fp=fopen($countfile,"r"); $num=fgets($fp,20); $num=$num+1; //每次其值自动加一 fclose($fp); $fp=fopen($countfile,"w"); fwrite($fp,$num); //更新其值 fclose($fp); //利用上面自动计数的值获得HTML的路径$path $houzui=”.html”; $path=$num.$houzui; //这样形成的路径是自动增长的,如1.html,2.html,3.html……….添加一条新闻便自动加上1 //以下用SQL语句添加数据至表 news $sql=”insert into news (title,content,path) values (‘”.$title.”’,’”.$content.”’,’”.$path.”’)”; $query=mysql_query($sql); //以下为关键之处,把从表单获得的数据替换模板中的{title},{content}标记 $fp=fopen(“model.htm”,”r”) //只读打开模板 $str=fread($fp,filesize(“mode.htm”));//读取模板中内容 $str=str_replace(“{title}”,$title,$str); $str=str_replace(“{content}”,$content,$str);//替换内容 fclose($fp); $handle=fopen($path,”w”); //写入方式打开新闻路径 fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件 fclose($handle); //收尾工作: echo “<a href=$path target=_blank>查看刚才添加的新闻</a>”; ?>
OK, the entire sample source code for generating HTML is here. The key is to use the replacement method. $str=str_replace("{replaced content}",$replaced content,$str);
Therefore, to summarize the above approach: first design the news template and put the information that needs to be replaced Use { } to put the content in the corresponding position in the template, then design the form, and then the final form handler, replace the variables obtained from the form with the corresponding content in the template, so that different HTML will be generated every time; The same is true if you need to modify the HTML content. After obtaining the modified form content, first update the database with the update statement, and then replace the content in the template; if you want to delete, first delete the content to be deleted in the table, and then use unlink($path) to delete the physical file of HTML.
php htmlspecialchars example code detailed explanation
php HtmlReplace input filtering security function example code
Recommended 10 articles about the php html_entity_decode() function
The above is the detailed content of Ideas on how PHP generates HTML. For more information, please follow other related articles on the PHP Chinese website!