Heim >Backend-Entwicklung >PHP-Tutorial >PHP生成HTML前提条件及原理介绍_PHP教程
很多网站都开始使用
笔者的网站曾经使用PHP这一动态技术来构建新闻发布系统,其原理也就是应用了PHP生成HTML静态页面的技术,相关平台是 Windows XP Sp2+php4.32+mysql,因此,在这里,想简单地谈一下这种做法的思路。
这篇文章适合于对PHP+MYSQL数据库操作,SQL语句以及网页设计有点基础的朋友,如果您是一个从头开始学的朋友,那么请先打好基础吧!到这里就不用往下看了。如果您都符合上述条件的话,那么恭喜您,请接着往下看。但是,在具体动手构建PHP生成HTML之前,您还要做好以下几点准备工作。
一、 PHP生成HTML需要具备本地调试PHP的功能
在WINDOWS XP操作系统下,笔者建议你可以到网上下载一个PHP+MYSQL+APHCHE的服务器套装,如华军软件园,去那里搜索一下就可以下载到。下载完后默认安装即可,这样您就具备了在本地测试PHP的功能了,省去了很多手动配置的麻烦,怎么样,简单吧,OK,这仅仅只是第一步。
二、 PHP生成HTML还要构思新闻发布系统所具备的功能
首页的新闻发布往往是通过后台更新的,后台的更新无非是由添加、编辑、删除数据等基本的功能所实现的。在这里,你可以使用网页设计软件来构建你的想要的后台界面,其功能的实现当然是要用到PHP喽。这一步建议您先想好新闻发布系统要有的功能。在这里,如何用PHP添加、编辑、删除数据就不再重复,因为重点是如何在这基础上生成静态技术。
三、 PHP生成HTML的技术原理。
哈哈。费话说了那么多,终于到了该讲的地方了。其实,这一原理并不复杂,综合地来讲的话,应该是PHP中一个替换数据语法的应用。OK,讲一个简单范例吧,一步一步地分析!相信聪明的你能看得明白的咯,仔细看好每一步即可,在这里,只是引导大家如何做而已,具体的可以实践一下!
(1)在MYSQL里新建一数据库,命名为 database (可自定义),新建一表,命名为 news(因为是新闻发布嘛,取个好记的名字即可,可自定义),然后建立这几个字段名:
id (自动递增,这是关键,类型:INT)
title (顾名思义,新闻标题,类型可取 TEXT)
content (新闻内容,类型可取 TEXT)
path (HTML文件路径,类型可取 TEXT)
(2)建立 conn.php
这是连接数据库的PHP文件,你可以把连接数据的语句单独放在这一文件里,以后多个需要连接数据库的文件直接引用这个文件即可。
(3)设计添加新闻的表格 add.form 简单的源代码如下:
新闻标题:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><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></span></li> <li class="alt"><span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span></span></span></li> <li> <span>新闻内容:</span><span class="tag"><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></span> </li> <li><span class="tag"><span> /textarea</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span></span></span></li> <li class="alt"><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=”submit” </span></span></li> <li><span class="tag"><span> /form</span><span class="tag">></span><span> </span></span></li> </ol>
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><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></span></li> <li> <span>新闻标题:</span><span class="tag"><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 class="tag-name">br</span><span class="tag">></span><span> </span></span></span> </li> <li class="alt"> <span>新闻内容:</span><span class="tag"><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> /textarea</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span></span></span></span> </li> <li><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=”submit” </span></span></li> <li class="alt"><span class="tag"><span> /form</span><span class="tag">></span><span> </span></span></li> </ol>
(4)建立一个PHP生成HTML的模板,另存为model.htm,和 add.php可以在同一目录下。
示例源代码:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> </span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></span></li> <li><span class="tag"><span> </span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span>此新闻的标题:{title} </span></li> <li><span>此新闻的内容:{content} </span></li> <li class="alt"><span class="tag"><span> /body</span><span class="tag">></span><span> </span></span></li> <li><span class="tag"><span> /html</span><span class="tag">></span><span> </span></span></li> </ol>
{ }大括号内的内容即是要被替换的内容,整个静态模板的设计可以根据自己的思路,但{ }内被替换的内容必须包含在内,如上面的{title},{content};咔咔~简单地说,设计好一个很好看的新闻模板后,把要被替换的如{title},{content}等标记放到需要的地方就可以了撒。
(5)详解 add.php 源码
<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> ?php </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,整个生成HTML的示例源码就到这里,其关键是用了替换的方法。
$str=str_replace(“{被替换的内容}”,$替换的内容,$str);
因此,总结一下以上的PHP生成HTML做法:先设计好新闻模板,把需要被替换的内容用{ }放到模板中相应的位置,然后设计表单,再是最后的表单处理程序,把从表单中获取的变量替换模板中相应的内容即可,这样每次都会生成不同的HTML;
如果需要修改HTML的内容也是一样的,获得修改后的表单内容后,先用 update 语句更新数据库,再重新替换一下模板中的内容即可;删除的话,先delete表中要删除的内容,再用unlink($path) 来删除HTML的物理文件即可。