先建立一chtml数据库 表名称为bihtml
create table bihtml (
id int(11) auto_increment not null,
szdtitle varchar(30),
szdcontent text
primary key(id)
)
在表中插入两条记录
insert into 'bihtml'('id','szdtitle','szdcontent') values(null, '测试静态页面','测试静态页面内容');
insert into 'bihtml'('id','szdtitle','szdcontent') values(null, '测试静态页面2','测试静态页面内容2');
一般生成静态页面程序要设计到缓存处理用到函数ob_start();打开缓存 ob_get_contents获取缓存内容 ob_end_clean清除缓存内容,还有函数flush ob_get_length ob_end-flush等。
建立文件程序
function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content)
{
if (is_file ($file_cjjer_name)){
@unlink ($file_cjjer_name);
}
$cjjer_handle = fopen ($file_cjjer_name,"w");
if (!is_writable ($file_cjjer_name)){
return false;
}
if (!fwrite ($cjjer_handle,$file_cjjer_content)){
return false;
}
fclose ($cjjer_handle); //关闭指针
return $file_cjjer_name;
}
ob_start();
$id=$_POST['id'];
if(isset($id)&&is_integer($id))
{
$conn=mysql_connect("localhost","root","") or die("can not link the server ");
mysql_select_db("chtml",$conn) or die("can not link the database");
$sql="select * from bihtml where id='$id'";
$result=mysql_query($sql);
$arr=mysql_fetch_array($result);
$output=$arr["szdtitle"].$arr["szdcontent"];
print $output;
$this_my_f= ob_get_contents(); //此处关键
ob_end_clean();
$filename =$id.".html";
$strQuery="update bihtml set filename='$filename' where id=".$id;
if(mysql_query($strQuery))
echo "更新成功<br>";
else
echo "更新失败<br>";
if(tohtmlfile_cjjer($filename,$this_my_f))
echo "生成成功 $filename";
else
echo "生成识别";
}
显示列表程序
<?
$conn=mysql_connect("localhost","root","") or die("can not link the server ");
mysql_select_db("chtml",$conn) or die("can not link the database");
$sql="select * from bihtml";
$result=mysql_query($sql);
while($arr=mysql_fetch_array($result))
{
$filename=$arr['filename'];
$title=$arr['szdtitle'];
print("<br><a href =$filename>$title</a>");
}
mysql_free_result($result);
?>
刚刚测试的php生成静态页程序(实例)
最新推荐文章于 2024-08-01 17:54:23 发布