下面以一个新闻系统为例,讲解静态化页面,简单的HTML模板文件不再提:
首先看新建新闻页面,如下:
<?php
//替换函数
function model_replace($row,$title="",$content=""){
$row=str_replace("%title%",$title,$row);
$row=str_replace("%content%",$content,$row);
return $row;
}
//2.1 把新闻添加到数据库
$new_title=$_POST['title'];
$new_content=$_POST['content'];
//关键因为你下面要把这个新闻-》html页面,因此要把这个文件名记录.
//产生一个文件名
$file_name='s'.date('YmdHis').rand(1,1000).'.html';
//保存到数据库
//连接数据库,并取出信息
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die('失败');
}
mysql_select_db("test",$conn);
mysql_query("set names utf8");
$sql="insert into news (title,content,fname) values('$new_title','$new_content','$file_name')";
if(mysql_query($sql)){
//2.2 动态产生一个这条新闻,对应的静态页面
//先编写一个漂亮的显示新闻细节的模板文件 news_temp.html
//打开模板文件
$fp_news_temp=fopen("news_temp.html","r");
//创建新文件
$fp_news_detail=fopen($file_name,"w");
//一行一行读取news_temp.html替换
while(!feof($fp_news_temp)){
//从模板文件读一行
$row=fgets($fp_news_temp);
//交给一个函数,处理
$row=model_replace($row,$new_title,$new_content);
fwrite($fp_news_detail,$row);
}
//关闭资源
fclose($fp_news_temp);
fclose($fp_news_detail);
header("Location: ok.html");
}else{
echo "添加新闻失败";
}
exit();
下面是显示新闻列表页面,重点注意查看新闻的链接是指向一个静态网址
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<a href="add_news.html">添加新闻</a>
<hr/>
<?php
//这个页面我们显示所有的新闻
//连接数据库(mvc) SqlHelper
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die('失败');
}
mysql_select_db("test",$conn);
mysql_query("set names utf8");
$sql="select * from news";
$res=mysql_query($sql,$conn);
echo "<table><tr><td>id</td><td>标题</td><td>查看详情</td><td>修改新闻</td><tr/>";
while($row=mysql_fetch_row($res)){
//传统做法[没有页面静态]
//echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td><a href='newsDetail.php?id={$row[0]}'>点击查看详情</a></td></tr>";
echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td><a href='$row[3]' >点击查看详情</a></td><td><a href='update_news.php?id={$row[0]}'>修改新闻</a></td></tr>";
}
echo "</table>";
mysql_free_result($res);
mysql_close($conn);
?>
</html>
当收到修改页面请求时,同时修改数据库和更换静态页面(因为修改静态页面太麻烦,不如新建一个)
<?php include_once 'tools.php'; //获取新闻id if(!empty($_POST['id'])){ $id=$_POST['id']; } $file_name=@$_POST['filename']; $title=@$_POST['title']; $content=@$_POST['content']; //创建一个新的静态文件名. $new_file_name='s'.date('YmdHis').rand(1,1000).".html"; //更新数据库. $sql="update news set title='$title' ,content='$content' ,fname='$new_file_name' where id=$id "; $conn=mysql_connect("localhost","root","root"); if(!$conn){ die('失败'); } mysql_select_db("test",$conn); mysql_query("set names utf8"); if(mysql_query($sql,$conn)){ //数据更新ok/更新的静态页面 unlink($file_name); //打开模板 $file_model=fopen("news_temp.html","r"); $new_file=fopen($new_file_name,"w"); //一行一行读取 while(!feof($file_model)){ $row=fgets($file_model); //更新(因为模板文件没有变化,替换函数可以照用) $row=model_replace($row,$title,$content); //添加到新的静态文件 fwrite($new_file,$row); } //关闭资源 fclose($file_model); fclose($new_file); //关闭连接 mysql_close($conn); header("Location:ok.html"); }else{ //关闭连接 mysql_close($conn); header("Location:error.html"); }
?>
//符上一个tools.php,用于将模板中的标识替换为变量
<?php //替换函数 function model_replace($row,$title="",$content=""){ $row=str_replace("%title%",$title,$row); $row=str_replace("%content%",$content,$row); return $row; }