php 利用数据库教程信息生成静态页面实例代码
2010-07-27 17:10
936 查看
php教程 利用数据库教程信息生成静态页面实例代码
这是一款用户自己写的php生成html页面的代码,简单实用比较适合初学者,如果你正在学习php生成静态文件,这一个实例从数据库到php到模板文件都一步步的很清楚哦。
$con=mysql教程_connect("localhost","root","")or die("链接错误!!!");
mysql_select_db("message",$con)or die("数据库链接错误!!!");
mysql_query("set names 'gbk'");
$sql=mysql_query("select * from message");
if($_post[button]){
while($row=mysql_fetch_array($sql)){
$path=$row[0].'.html';
$user=$row[1];
$title=$row[2];
$content=$row[3];
$date=$row[4];
$fp=fopen("moban/html.html","r");
$str=fread($fp,filesize("./moban/html.html"));
$str=str_replace("{user}",$user,$str);
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content}",$content,$str);
$str=str_replace("{date}",$date,$str);
fclose($fp);
$handle=fopen("news/".$path,"w");
fwrite($handle,$str);
fclose($handle);
echo "<script>alert('生成成功!!!');window.location.href='html.php';</script>";
}
}
?>
<!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=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="button" id="button" value="生成静态页面" />
</label>
</form>
</body>
</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.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<table width="500">
<tr>
<td width="80"><div align="right">名称:</div></td>
<td width="408"><div align="left">{user}</div></td>
</tr>
<tr>
<td><div align="right">标题:</div></td>
<td><div align="left">{title}</div></td>
</tr>
<tr>
<td><div align="right">内容:</div></td>
<td><div align="left">{content}</div></td>
</tr>
<tr>
<td><div align="right">日期:</div></td>
<td><div align="left">{date}</div></td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><div align="left"></div></td>
</tr>
</table>
</body>
删除html代码
<?php
$con=mysql_connect("localhost","root","")or die("链接错误!!!");
mysql_select_db("message",$con)or die("数据库链接错误!!!");
mysql_query("set names 'gbk'");
$sql=mysql_query("select * from message");
if($_post[button]){
while($row=mysql_fetch_array($sql)){
$path=$row[0].'.html';
if(@unlink("news/".$path)){
echo "<script>alert('删除成功!!!');window.location.href='unlink_html.php';</script>";
}else{
echo "<script>alert('无内容可以删除!!!');window.location.href='unlink_html.php';</script>";
}
}
}
?>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="button" id="button" value="删除静态页面" />
</label>
</form>
//数据库结构,用phpmyadmin导入就行了
表的结构 `message`
--
create table `message` (
`id` int(4) not null auto_increment,
`user` varchar(20) not null,
`title` varchar(100) not null,
`content` text not null,
`date` date not null,
primary key (`id`)
) engine=myisam default charset=gbk auto_increment=3 ;
--
-- 导出表中的数据 `message`
--
insert into `message` (`id`, `user`, `title`, `content`, `date`) values
(1, 'dfkk', '数据库信息生成静态页面', 'www.111cn.net', '2010-02-12'),
(2, 'dfkk', '数据库信息生成静态页面', '原理很简单', '2010-02-12');
自己写的,可通过按钮生成静态页面和删除已生成的静态页面。
moban存放模板文件夹(勿删)。
news生成静态页面文件夹(勿删)。
注意事项:phpmyadmin工具导入数据库文件message.sql。
这是一款用户自己写的php生成html页面的代码,简单实用比较适合初学者,如果你正在学习php生成静态文件,这一个实例从数据库到php到模板文件都一步步的很清楚哦。
$con=mysql教程_connect("localhost","root","")or die("链接错误!!!");
mysql_select_db("message",$con)or die("数据库链接错误!!!");
mysql_query("set names 'gbk'");
$sql=mysql_query("select * from message");
if($_post[button]){
while($row=mysql_fetch_array($sql)){
$path=$row[0].'.html';
$user=$row[1];
$title=$row[2];
$content=$row[3];
$date=$row[4];
$fp=fopen("moban/html.html","r");
$str=fread($fp,filesize("./moban/html.html"));
$str=str_replace("{user}",$user,$str);
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content}",$content,$str);
$str=str_replace("{date}",$date,$str);
fclose($fp);
$handle=fopen("news/".$path,"w");
fwrite($handle,$str);
fclose($handle);
echo "<script>alert('生成成功!!!');window.location.href='html.php';</script>";
}
}
?>
<!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=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="button" id="button" value="生成静态页面" />
</label>
</form>
</body>
</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.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<table width="500">
<tr>
<td width="80"><div align="right">名称:</div></td>
<td width="408"><div align="left">{user}</div></td>
</tr>
<tr>
<td><div align="right">标题:</div></td>
<td><div align="left">{title}</div></td>
</tr>
<tr>
<td><div align="right">内容:</div></td>
<td><div align="left">{content}</div></td>
</tr>
<tr>
<td><div align="right">日期:</div></td>
<td><div align="left">{date}</div></td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><div align="left"></div></td>
</tr>
</table>
</body>
删除html代码
<?php
$con=mysql_connect("localhost","root","")or die("链接错误!!!");
mysql_select_db("message",$con)or die("数据库链接错误!!!");
mysql_query("set names 'gbk'");
$sql=mysql_query("select * from message");
if($_post[button]){
while($row=mysql_fetch_array($sql)){
$path=$row[0].'.html';
if(@unlink("news/".$path)){
echo "<script>alert('删除成功!!!');window.location.href='unlink_html.php';</script>";
}else{
echo "<script>alert('无内容可以删除!!!');window.location.href='unlink_html.php';</script>";
}
}
}
?>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="button" id="button" value="删除静态页面" />
</label>
</form>
//数据库结构,用phpmyadmin导入就行了
表的结构 `message`
--
create table `message` (
`id` int(4) not null auto_increment,
`user` varchar(20) not null,
`title` varchar(100) not null,
`content` text not null,
`date` date not null,
primary key (`id`)
) engine=myisam default charset=gbk auto_increment=3 ;
--
-- 导出表中的数据 `message`
--
insert into `message` (`id`, `user`, `title`, `content`, `date`) values
(1, 'dfkk', '数据库信息生成静态页面', 'www.111cn.net', '2010-02-12'),
(2, 'dfkk', '数据库信息生成静态页面', '原理很简单', '2010-02-12');
自己写的,可通过按钮生成静态页面和删除已生成的静态页面。
moban存放模板文件夹(勿删)。
news生成静态页面文件夹(勿删)。
注意事项:phpmyadmin工具导入数据库文件message.sql。
相关文章推荐
- php利用数据库教程信息生成静态页面实例代码
- PHP代码为什么不能直接保存HTML文件——>PHP生成静态页面教程
- php利用soap实现调用获取返回信息的代码实例
- PHP代码为什么不能直接保存HTML文件——>PHP生成静态页面教程
- PHP无限级分类生成树实例代码
- Apache Shiro+SpringMVC+Hibernate Search+Hibernate+Bootstrap企业信息管理系统基础框架搭建整合实例代码教程
- 利用PHP为大图片生成缩略图代码实现
- Jooq根据数据库表自动生成代码教程及配置
- PHP生成HTML静态页面实例代码
- 利用php访问数据库数据表,生成javabean.java文件,符合javabean语法
- 比较详细PHP生成静态页面教程
- PHP随机数生成代码与使用实例分析
- PHP分多步骤填写发布信息的简单方法实例代码
- PHP实现Webservice的实例代码调用教程
- php生成xml简单实例代码
- 利用Python爬取微博数据生成词云图片实例代码
- PHP生成迅雷、快车、旋风等软件的下载链接代码实例
- PHP生成静态HTML页面实例代码
- PHP的Yii框架中使用数据库的配置和SQL操作实例教程