Introduction to the prerequisites and principles of generating HTML with PHP_PHP Tutorial

WBOY
Release: 2016-07-15 13:34:21
Original
822 people have browsed it

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>=&rdquo;text&rdquo; </span><span class="attribute">size</span><span>=&rdquo;20&rdquo;</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>=&rdquo;10&rdquo; </span><span class="attribute">rows</span><span>=&rdquo;25&rdquo;</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>=&rdquo;submit&rdquo;   </span></li><li><span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

<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>=&rdquo;post&rdquo; <br /></span><span class="attribute">action</span><span>=&rdquo;add.php&rdquo;</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>=&rdquo;text&rdquo; <br /></span><span class="attribute">size</span><span>=&rdquo;20&rdquo;</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>=&rdquo;10&rdquo;<br /> </span><span class="attribute">rows</span><span>=&rdquo;25&rdquo;</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>=&rdquo;submit&rdquo;   </span></li><li class="alt"><span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

(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>
Copy after login

{ } 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>
Copy after login

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445996.htmlTechArticleMany websites have begun to use the author's website. I once used PHP, a dynamic technology, to build a news release system. The principle is also It uses PHP technology to generate HTML static pages. Related platforms...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!