有一个朋友问我怎么使用PHP生成HTML,我做了之后就把它写出来了。。。
CREATE TABLE IF NOT EXISTS `tdb_g` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`user` varchar(32) CHARACTER SET gbk NOT NULL,
`title` varchar(50) CHARACTER SET gbk NOT NULL,
`content` text CHARACTER SET gbk NOT NULL,
`gdatetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=64 ;
---------------------------------------------------------------------------
+++++++++++++++++++++++++++++++++++++++++
conn.php
$conn = @ mysql_connect("localhost", "root", "123456") or die("数据库链接错误");
mysql_select_db("guest", $conn);
mysql_query("set names 'GBK'");
+++++++++++++++++++++++++++++++++++++
index.php
<?php
date_default_timezone_set("PRC");//设置中国时间
include 'conn.php';//数据库连接文件
$dates = date("YmdHm");//这个时间做为生成HTML的文件名
$sql="select * from tdb_g";//这个是表名
$query=mysql_query($sql);
while ($rs=mysql_fetch_array($query)){
$user = $rs['user'];//取数据库中的用户
$title = $rs['title'];//取数据库中的标题
$content = $rs['content'];//取数据库中的内容
$files="test.html";//模板文件
$fp=fopen($files, "r");//打开文件,只读
$str=fread($fp, filesize($files));
$strs = str_replace("{title}", $title, $str);
$strs = str_replace("{content}", $content, $strs);
$strs = str_replace("{user}", $user, $strs);
fclose($fp);
$newfile = "./news/".$dates.$rs['id'].".html";//在news文件夹下生成HTML的文件
$hands = fopen($newfile, w);//写入
fwrite($hands, $strs);
$newsname = substr($newfile, 2); //截取文件名
$linkname = substr($newsname, 5); //截取
$link = "<a href=".$newsname.">".$title."</a>";
fclose($hands);
echo $link."<br />";//输出生成后的链接,显示在页面
}
mysql_close();
?>
-------------------------------------------------------------------
文件结构图
____________________________
Index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>标题:{title}</title>
</head>
<body>
静态网页生成:{content}...{user}<br />
</body>
</html>
________________________________
绿色字体为源码