標籤:通過 where star new pre == for 模板 批量
它只讓你修改頁面的某一部分,當然這“某一部分”是由你來確定的。美工先做好一個頁面,然後我們把這個頁面當作模板(要注意的是這個模板就沒必要使用EditRegion3這樣的代碼了,這種代碼是Dreamwerver為了方便自己設計而弄的標識),把這個模板中我們需要改變的地方用一個與HTML可以區分的字元代替,如“{title}”、“[title]”。在產生靜態頁面的時候只需要把資料和這些字串替換即可。這就是模板的含義了。
建立一個php頁面和一個html頁面[模板頁];注:如果是從資料庫調用資料,則將資料以數組的形式儲存,然後迴圈產生;
在php頁面,開啟html頁面->讀取html頁面的內容->替換參數->建立(開啟)一個新的html頁面->將替換的內容寫入新檔案中->關閉新檔案->產生成功;
$open = fopen("template.htm","r"); //開啟模板檔案$content = fread($open,filesize("template.htm")); //讀模數板檔案內容//print_r($content);$content = str_replace("{title}","測試標題",$content);//替換$content = str_replace("{contents}","測試內容",$content);$newtemp = fopen("1.htm","w");//產生,用寫入方式開啟一個不存在(新)的頁面fwrite($newtemp,$content);//將剛剛替換的內容寫入新檔案中fclose($newtemp);echo "產生";
php批量產生html測試:
//假設從資料庫中調的資料存放在二維數組$arr中$arr = array(array("新聞標題一","新聞內容一"),array("新聞標題二","新聞內容二")); foreach($arr as $key=>$value){ $title = $value[0]; $contents = $value[1]; //echo $title.‘‘.$contents.‘‘; $path = $key.‘.html‘; $open = fopen("template.htm","r"); //開啟模板檔案 $handle = fread($open,filesize("template.htm")); //讀模數板檔案內容 $content = str_replace("{title}",$title,$handle);//替換 $content = str_replace("{contents}",$contents,$handle); $newtemp = fopen($path,"w");//用寫入方式開啟一個不存在(新)的頁面 fwrite($newtemp,$content);//將剛剛替換的內容寫入新檔案中 fclose($newtemp); echo "產生";}
分頁問題:
如我們指定分頁時,每頁20篇。某子頻道列表內文章經資料庫查詢為45條,則,首先我們通過查詢得到如下參數:1,總頁數;2,每頁篇數。第二步,for ($i = 0; $i < allpages; $i++),頁面元素擷取,分析,文章產生,都在此迴圈中執行。不同的是,die ("建立檔案".$filename."成功!";這句去掉,放到迴圈後的顯示,因為該語句將中止程式執行。例:
$fp= fopen ("temp.html","r");$content= fread ($fp,filesize ("temp.html"));$onepage= ’20’;$sql = "select id from article where channel=’$channelid’";$query = mysql_query ($sql);$num = mysql_num_rows ($query);$allpages= ceil ($num / $onepage);for ($i = 0;$i<$allpages; $i++){ if ($i == 0){ $indexpath = "index.html"; } else { $indexpath = "index_".$i."html"; } $start = $i * $onepage; $list = ’’; $sql_for_page = "select name,filename,title from article where channel=’$channelid’ limit $start,$onepage"; $query_for_page = mysql_query ($sql_for_page); while ($result = $query_for_page){ $list .= ’<a href=’.$root.$result[’filename’].’ target=_blank>’.$title.’</a><br>’; } $content = str_replace ("{articletable}",$list,$content); if(is_file ($indexpath)){ @unlink ($indexpath); //若檔案已存在,則刪除 } $handle = fopen ($indexpath,"w"); //開啟檔案指標,建立檔案 if (!is_writable ($indexpath)){ echo "檔案:".$indexpath."不可寫,請檢查其屬性後重試!"; //修改為echo } if (!fwrite ($handle,$content)){ //將資訊寫入檔案 echo "組建檔案".$indexpath."失敗!"; //修改為echo } fclose ($handle); //關閉指標}fclose ($fp);die ("產生分頁檔案完成,如產生不完全,請檢查檔案許可權系統後重建!");
html模板產生靜態頁面及模板分頁處理