php生成靜態頁

先建立一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);
 ?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章