Foreword:
At present, the news release systems of many websites on the Internet use dynamic server technology to generate static HTML. The advantages of this are: first, it can reduce the burden on their servers, and second, because HTML static pages are generated, their The website has a greater chance of being found 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 simply Let’s 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 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, search there and you can download it. 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. Conceive the functions of the news release system
The news release on the homepage is often updated through the background. The background updates are nothing more than basic functions such as adding, editing, and deleting data. 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.
三、 PHP生成HTML的技术原理。
哈哈。费话说了那么多,终于到了该讲的地方了。其实,这一原理并不复杂,综合地来讲的话,应该是PHP中一个替换数据语法的应用。OK,讲一个简单范例吧,一步一步地分析!相信聪明的你能看得明白的咯,仔细看好每一步即可,在这里,只是引导大家如何做而已,具体的可以实践一下!
(1)在MYSQL里新建一数据库,命名为 database (可自定义),新建一表,命名为 news(因为是新闻发布嘛,取个好记的名字即可,可自定义),然后建立这几个字段名:
id (自动递增,这是关键,类型:INT)
title (顾名思义,新闻标题,类型可取 TEXT)
content (新闻内容,类型可取 TEXT)
path (HTML文件路径,类型可取 TEXT)
(2)建立 conn.php
这是连接数据库的PHP文件,你可以把连接数据的语句单独放在这一文件里,以后多个需要连接数据库的文件直接引用这个文件即可。
(3)设计添加新闻的表格 add.form 简单的源代码如下:
代码拷贝框
[Ctrl+A 全部选择 然后拷贝]
(4)建立一个 HTML 的模板,另存为model.htm,和 add.php可以在同一目录下。
示例源代码:
(5)详解 add.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);
代码拷贝框
//Use the automatically counted value above to obtain the HTML path $path
$houzui=”.html”;
$path=$num.$houzui;
//This is formed The path is automatically increased, such as 1.html, 2.html, 3.html…………. Adding a news will automatically add 1
//The following uses SQL statements to add data to the table news
$sql="insert into news (title,content,path) values ('".$title."','".$content."','".$path."')";
$query=mysql_query($sql);
//The following is the key point, replace the {title}, {content} tags in the template with the data obtained from the form $fp=fopen(“model.htm”,”r”) //Only Read and open the template
$str=fread($fp,filesize(“mode.htm”));//Read the content in 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 in writing mode
fwrite($handle,$str); //Write the content just replaced into the generated HTML file
fclose($handle);
Code copy box
[Ctrl+A select all and copy]
//Finishing work:
echo “View the news just added a>”;
Code copy box
[Ctrl+A select all and then copy]
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 content 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 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; 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.