


Introduction to the prerequisites and principles of generating HTML with PHP_PHP Tutorial
Many websites have begun to use
The author's website once used PHP, a dynamic technology, to build a news release system. The principle is to use PHP to generate HTML static pages. The technology, the relevant platform is Windows XP Sp2+php4.32+mysql, so here, I would like to briefly talk about 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 is learning 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 you actually build PHP to generate HTML, you need to make the following preparations.
1. PHP generating HTML requires the ability to debug PHP locally
Under the WINDOWS XP operating system, the author recommends that you download PHP+MYSQL+APHCHE from the Internet Server packages, such as Huajun Software Park, can be downloaded by searching there. After downloading, you can install it by default. In this way, you will have the function of testing PHP locally, saving a lot of trouble of manual configuration. How about, simple, OK, this is just the first step.
2. PHP generates HTML and also conceives the functions of the news release system
News releases on the homepage are often updated through the background, and the updates in the background are nothing more than Basic functions such as adding, editing, and deleting data are implemented. Here, you can use web design software to build the backend interface you want. Of course, PHP is used to realize 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.
Haha. 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, the type can be TEXT)
path (HTML file path, the type can be TEXT)
(2) Create conn.php
This is the PHP file to connect to the database, you can put the statement to connect the data Put it in this file alone. In the future, multiple files that need to connect to the database can directly reference this file.
(3) Design the add.form form for adding news. The simple source code is as follows:
News title:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=”text” </span><span class="attribute">size</span><span>=”20”</span><span class="tag">></span></span></li> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span></span></li> <li> <span>新闻内容:</span><span class="tag"><</span><span> </span><span class="tag-name">textarea</span><span> <br /></span><span class="attribute">cols</span><span>=”10” </span><span class="attribute">rows</span><span>=”25”</span><span class="tag">></span> </li> <li> <span class="tag"><</span><span> /textarea</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=”submit” </span></li><li><span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span> </li> </ol>
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">form</span><span> </span><span class="attribute">method</span><span>=”post” <br /></span><span class="attribute">action</span><span>=”add.php”</span><span class="tag">></span><span> <br>//提交至 add.php </span></span></li> <li> <span>新闻标题:</span><span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=”text” <br /></span><span class="attribute">size</span><span>=”20”</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span>新闻内容:</span><span class="tag"><</span><span> </span><span class="tag-name">textarea</span><span> </span><span class="attribute">cols</span><span>=”10”<br /> </span><span class="attribute">rows</span><span>=”25”</span><span class="tag">></span><span class="tag"><</span><span> /textarea</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=”submit” </span></li><li class="alt"><span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span> </li> </ol>
(4) Create a template for PHP to generate HTML, save it as model.htm, and it can be in the same directory as add.php.
Sample source code:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li> <li> <span class="tag"><</span><span> </span><span class="tag-name">body</span><span class="tag">></span><span> </span> </li> <li class="alt"><span>此新闻的标题:{title} </span></li> <li><span>此新闻的内容:{content} </span></li> <li class="alt"> <span class="tag"><</span><span> /body</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> /html</span><span class="tag">></span><span> </span> </li> </ol>
{ } 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 content to be replaced within { } Must be included, such as {title}, {content} above; Kaka~ Simply put, after designing a good-looking news template, put the tags to be replaced, such as {title}, {content}, etc. Just spread it wherever needed.
(5) Detailed explanation of add.php source code
<ol class="dp-xml"> <li class="alt"><span><span>require_once(“conn.php”); </span></span></li> <li><span> //引用conn.php,连接数据库 </span></li> <li class="alt"> <span>$</span><span class="attribute">title</span><span>=$_POST[“title”]; </span> </li> <li> <span>$</span><span class="attribute">content</span><span>=$_POST[“content”]; </span> </li> <li class="alt"><span>//获得表单变量 </span></li> <li><span>//以下建立一文本文档,其值自动计数 </span></li> <li class="alt"> <span>$</span><span class="attribute">countfile</span><span>=</span><span class="attribute-value">"count.txt"</span><span>; </span> </li> <li><span>if(!file_exists($countfile)) </span></li> <li class="alt"><span>{ </span></li> <li><span>fopen($countfile,"w"); / </span></li> <li class="alt"><span>/如果此文件不存在,则自动建立一个 </span></li> <li><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute">fp</span><span>=</span><span class="attribute-value">fopen</span><span>($countfile,"r"); </span> </li> <li> <span>$</span><span class="attribute">num</span><span>=</span><span class="attribute-value">fgets</span><span>($fp,20); </span> </li> <li class="alt"> <span>$</span><span class="attribute">num</span><span>=$num+1; //每次其值自动加一 </span> </li> <li><span>fclose($fp); </span></li> <li class="alt"> <span>$</span><span class="attribute">fp</span><span>=</span><span class="attribute-value">fopen</span><span>($countfile,"w"); </span> </li> <li><span>fwrite($fp,$num); //更新其值 </span></li> <li class="alt"><span>fclose($fp); </span></li> <li>< <span> ?php </span> </li> <li class="alt"><span>require_once(“conn.php”); </span></li> <li><span>//引用conn.php,连接数据库 </span></li> <li class="alt"> <span>$</span><span class="attribute">title</span><span>=$_POST[“title”]; </span> </li> <li> <span>$</span><span class="attribute">content</span><span>=$_POST[“content”]; </span> </li> <li class="alt"><span> //获得表单变量 </span></li> <li> <span>$</span><span class="attribute">countfile</span><span>=</span><span class="attribute-value">"count.txt"</span><span>; </span> </li> <li class="alt"><span>if(!file_exists($countfile)) </span></li> <li><span>{ </span></li> <li class="alt"><span>fopen($countfile,"w"); </span></li> <li><span> //如果此文件不存在,则自动建立一个 </span></li> <li class="alt"><span>} </span></li> <li> <span>$</span><span class="attribute">fp</span><span>=</span><span class="attribute-value">fopen</span><span>($countfile,"r"); </span> </li> <li class="alt"> <span>$</span><span class="attribute">num</span><span>=</span><span class="attribute-value">fgets</span><span>($fp,20); </span> </li> <li> <span>$</span><span class="attribute">num</span><span>=$num+1; //每次其值自动加一 </span> </li> <li class="alt"><span>fclose($fp); </span></li> <li> <span>$</span><span class="attribute">fp</span><span>=</span><span class="attribute-value">fopen</span><span>($countfile,"w"); </span> </li> <li class="alt"><span>fwrite($fp,$num); //更新其值 </span></li> <li><span>fclose($fp); </span></li> <li class="alt"> <span>$</span><span class="attribute">houzui</span><span>=”.html”; </span> </li> <li> <span>$</span><span class="attribute">path</span><span>=$num.$houzui; </span> </li> <li class="alt"><span>//这样形成的路径是自动增长的,如1.html,<br>2.html,3.html……….添加一条新闻便自动加上1 </span></li> <li><span>//以下用SQL语句添加数据至表 news </span></li> <li class="alt"> <span>$</span><span class="attribute">query</span><span>=</span><span class="attribute-value">mysql_query</span><span>($sql); </span> </li> <li><span>//以下为关键之处,把从表单获得的数据替换<br>模板中的{title},{content}标记 </span></li> <li class="alt"> <span>$</span><span class="attribute">str</span><span>=</span><span class="attribute-value">fread</span><span>($fp,filesize(“mode.htm”)); </span> </li> <li><span>//读取模板中内容 </span></li> <li class="alt"> <span>$</span><span class="attribute">str</span><span>=</span><span class="attribute-value">str_replace</span><span>(“{title}”,$title,$str); </span> </li> <li> <span>$</span><span class="attribute">str</span><span>=</span><span class="attribute-value">str_replace</span><span>(“{content}”,$content,$str); </span> </li> <li class="alt"><span>//替换内容 </span></li> <li><span>fclose($fp); </span></li> <li class="alt"> <span>$</span><span class="attribute">handle</span><span>=</span><span class="attribute-value">fopen</span><span>($path,”w”); </span> </li> <li><span>//写入方式打开新闻路径 </span></li> <li class="alt"><span>fwrite($handle,$str); </span></li> <li><span>//把刚才替换的内容写进生成的HTML文件 </span></li> <li class="alt"><span>fclose($handle); </span></li> <li> <span>$</span><span class="attribute">fp</span><span>=</span><span class="attribute-value">fopen</span><span>(“model.htm”,”r”) </span> </li> <li class="alt"><span>//只读打开模板 </span></li> <li> <span>$</span><span class="attribute">str</span><span>=</span><span class="attribute-value">fread</span><span>($fp,filesize(“mode.htm”)); </span> </li> <li class="alt"><span>//读取模板中内容 </span></li> <li> <span>$</span><span class="attribute">str</span><span>=</span><span class="attribute-value">str_replace</span><span>(“{title}”,$title,$str); </span> </li> <li class="alt"> <span>$</span><span class="attribute">str</span><span>=</span><span class="attribute-value">str_replace</span><span>(“{content}”,$content,$str); </span> </li> <li><span>//替换内容 </span></li> <li class="alt"><span>fclose($fp); </span></li> <li><span>fwrite($handle,$str); </span></li> <li class="alt"><span>//把刚才替换的内容写进生成的HTML文件 </span></li> <li><span>fclose($handle); </span></li> </ol>
OK, the entire sample source code for generating HTML ends here. The key is Used the substitution method.
$str=str_replace("{replaced content}",$replaced content,$str);
Therefore, to summarize the above PHP generated HTML method: first design the news template, and The content that needs to be replaced is placed in the corresponding position in the template using { }, and then the form is designed, and then the final form handler is used to replace the corresponding content in the template with the variables obtained from the form, so that different values will be generated each time. HTML;
The same is true if you need to modify the content of the HTML. After obtaining the modified form content, first update the database with the update statement, and then replace the content in the template again; if you want to delete, delete first To delete the content in the table, use unlink($path) to delete the physical HTML file.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.
