php產生靜態頁面的詳細教學

WBOY
發布: 2016-07-25 09:05:47
原創
901 人瀏覽過
  1. { title }
  2. this is a { file } file's templets
  3. this is a { file } file's templets
複製程式碼

PHP處理:  templetest.php
  1. $title = "測試模板";
  2. $file = "TwoMax Inter test templet,author:Matrix@Two_Max" ;
  3.  $fp = fopen ("temp.html","r");
  4. $content = fread ($fp,filesize ("temp.html"));
  5. $content .= str_replace ( "{ file }",$file,$content);
  6. $content .= str_replace ("{ title }",$title,$content);
  7. echo $content;
  8. ?>
複製程式碼

  範本解析處理,即將經PHP腳本解析處理後所得的結果填入(content)進範本的處理過程。通常藉助於模板類別。目前較流行的模板解析類別有phplib,smarty,fastsmarty等等。模板解析處理的原理通常為替換。也有些程式設計師習慣將判斷,循環等處理放進模板檔案中,用解析類別處理,典型應用為block概念,簡單來說即為一個循環處理。由PHP腳本指定循環次數,如何循環代入等,再由範本解析類別具體實作這些操作。

  如何用PHP產生靜態檔案。

  PHP產生靜態頁面並不是指PHP的動態解析,輸出HTML頁面,而是指用PHP建立HTML頁面。同時因為HTML的不可寫性,我們所建立的HTML若有修改,則需刪掉重新產生即可。 (當然你也可以選擇用正規修改,但個人認為那樣做倒不如刪掉重新生成來得快捷,有些得不償失。)

  用過PHP檔案運算子的PHP FANS知道,PHP中有一個檔案運算子fopen,也就是開啟檔案。若文件不存在,則嘗試建立。這即是PHP可以用來建立HTML檔案的理論基礎。只要用來存放HTML檔案的資料夾有寫入權限(即權限定義0777),即可建立檔案。 (針對UNIX系統而言,Win系統無須考慮。)仍上述例為例,若我們修改最後一句,並指定在test目錄下產生一個名為test.html的靜態檔案:
  1. $title = "測試模板";
  2. $file = "TwoMax Inter test templet,author:Matrix@Two_Max" ;
  3. $fp = fopen ("temp.html","r");
  4. $content = fread ($fp,filesize ("temp.html"));
  5. $content .= str_replace ( "{ file }",$file,$content);
  6. $content .= str_replace ("{ title }",$title,$content);
  7. // echo $content;
  8. $filename = "test/test.html";
  9. $handle = fopen ($filename,"w"); //開啟檔案指針,建立檔案
  10. /*
  11.  檢查檔案是否已建立且可寫入
  12. */
  13. if (!is_writable ($filename)){
  14. die ("檔案:".$filename."不可寫,請檢查其屬性後再試一次!");
  15. }
  16. if (!fwrite ($handle,$content)){ //將資訊寫入檔案
  17. die ("產生檔案".$filename."失敗!");
  18. }
  19. fclose ($ handle); //關閉指標
  20. die ("建立檔案".$filename."成功!");
?>
複製程式碼

常見問題解決方案參考: 一,文章列表問題: 在資料庫中建立字段,記錄文件名,每產生一個文件,將自動產生的文件名存入資料庫,對於推薦文章,只需指向存放靜態文件的指定資料夾中的該頁面即可。利用PHP操作處理文章列表,存為字串,生成頁面時替換此字串即可。如,在頁面中放置文章列表的表格加入標記{ articletable },而在PHP處理文件中:

  1. $title = "測試模板";
  2. $file = "TwoMax Inter test templet,author:Matrix@Two_Max" ;
  3. $fp = fopen ("temp.html","r");
  4. $content = fread ($fp,filesize ("temp.html"));
  5. $content .= str_replace ( "{ file }",$file,$content);
  6. $content .= str_replace ("{ title }",$title,$content);
  7. // 產生清單開始
  8. $list = ' ';
  9. $sql = "select id,title,filename from article";
  10. $query = mysql_query ($sql);
  11. while ($result = mysql_fetch_array ($query)){
  12. $
  13. $
  14. $
  15. $
  16. $
  17. $
  18. $
  19. $
  20. $
  21. $
  22. $
  23. $
  24. $ list .= ''.$result['title'].'';
  25. }
  26. $content .= str_replace ("{ articletable }",$list,$content);
  27. //產生列表結束
  28. // echo $content;
  29. $filename = "test/test.html";
  30. $handle = fopen ($filename,"w"); //開啟檔案指針,建立檔案
/*
 檢查檔案是否已建立且可寫*/if (!is_writable ($filename)){
die ("檔案:".$filename."不可寫,請檢查其屬性後再試一次! "失敗!");

}fclose ($handle); //關閉指標

die ("建立檔案".$filename."成功!");
?>
  1. 複製程式碼
  2. 二,分頁問題。   如我們指定分頁時,每頁20篇。某子頻道清單內文章經資料庫查詢為45條,則,首先我們透過查詢得到以下參數:1,總頁數;2,每頁篇數。第二步,for ($i = 0; $i
  3. $fp = fopen ("temp.html","r");
  4. $content = fread ($fp ,filesize ("temp.html"));
  5. $onepage = '20';
  6. $sql = "select id from article where channel='$channelid'";
  7. $query = mysql_query ($ sql);
  8. $num = mysql_num_rows ($query);
  9. $allpages = ceil ($num / $onepage);
  10. for ($i = 0;$iif ($i == 0){
  11. $indexpath = "index.html";
  12. } else {
  13. $indexpath = "index_".$i."html";
  14. }
  15. $start = $i * $onepage;
  16. $list = '';
  17. $sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage ";
  18. $query_for_page = mysql_query ($sql_for_page);
  19. while ($result = $query_for_page){
  20. $list .= ''.$title.'';
  21. }
  22. $list .= ''.$title.'';
  23. }
  24. $
  25. $ content = str_replace ("{ articletable }",$list,$content);
  26. if (is_file ($indexpath)){
  27. @unlink ($indexpath); //若檔案已存在,則刪除
  28. }
  29. $handle = fopen ($indexpath,"w"); //開啟檔案指針,建立檔案
  30. /*
  31.   檢查檔案是否已建立且可寫入
  32. */
if (!is_writable ($indexpath)){
echo "檔案:".$indexpath."不可寫,請檢查其屬性後再試一次!"; //修改為echo}if ( !fwrite ($handle,$content)){ //將資訊寫入檔案
echo "產生檔案".$indexpath."失敗! "; //修改為echo}fclose ($handle); //關閉指標}fclose ($fp);die ("生成分頁檔案完成,如生成不完全,請檢查檔案權限系統後重新產生!

其中如其它資料生成,資料輸入輸出檢查,分頁內容指向等可酌情在頁面中加入。

您可能感興趣的文章: php產生靜態頁面的三種方法與程式碼詳解 php產生靜態頁函數(php2html)的範例 php產生靜態頁面的方法(三個函數) 細說php產生靜態檔之模板與快取 php寫的一個產生靜態頁面的類別 虛擬主機上定時自動產生靜態頁面的方法 php產生靜態檔案的二種方法 php產生靜態html檔案的原理分析 smarty產生靜態頁面的方法 了解php產生靜態HTML檔的原理 PHP產生靜態頁面的方法 php產生靜態html檔案的三種方法



來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!