php產生靜態檔案分頁問題
來源:互聯網
上載者:User
如我們指定分頁時,每頁20篇。某子頻道列表內文章經資料庫查詢為45條,則,首先我們通過查詢得到如下參數:1,總頁數;2,每頁篇數。第二步,for ($i = 0; $i < allpages; $i++),頁面元素擷取,分析,文章產生,都在此迴圈中執行。不同的是,die ("建立檔案".$filename."成功!";這句去掉,放到迴圈後的顯示,因為該語句將中止程式執行。例:
Code:
$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 .= ''''.$title.''
'';
}
$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 ("產生分頁檔案完成,如產生不完全,請檢查檔案許可權系統後重建!");
?>
大致思路如此,其中如其它資料產生,資料輸入輸出檢查,分頁內容指向等可酌情在頁面中加入。
在實際文章系統處理過程當中,還有許多問題有待考慮,與動態網頁面不同之處,需注意的地方還有很多。但大致思路即是如此,其它方面可舉一反三而得。