使用相關(guān)函數(shù)實(shí)現(xiàn)PHP處理分頁
通過對(duì)PHP語言的深入學(xué)習(xí)可以知道,其是一個(gè)功能非常強(qiáng)大的語言,應(yīng)用領(lǐng)域也非常廣泛。比如今天我們介紹的分頁問題,利用PHP處理分頁,就能得到一個(gè)良好的結(jié)果。#t#
如我們指定分頁時(shí),每頁20篇。某子頻道列表內(nèi)文章經(jīng)數(shù)據(jù)庫查詢?yōu)?5條,則,首先我們通過查詢得到如下參數(shù):1,總頁數(shù);2,每頁篇數(shù)。
PHP處理分頁第二 步,for ($i = 0; $i < allpages; $i++),頁面元素獲取,分析,文章生成,都在此循環(huán)中執(zhí)行。不同的是,die ("創(chuàng)建文件".$filename."成功!";這句去掉,放到循環(huán)后的顯示,因?yàn)樵撜Z句將中止程序執(zhí)行。例:
- < ?php
- $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");
//打開文件指針,創(chuàng)建文件 - /*
- 檢查文件是否被創(chuàng)建且可寫
- */
- if (!is_writable ($indexpath)){
- echo "文件:".$indexpath."不可寫,
請(qǐng)檢查其屬性后重試!"; //修改為echo - }
- if (!fwrite ($handle,$content)){ //將信息寫入文件
- echo "生成文件".$indexpath."失?。?quot;; //修改為echo
- }
- fclose ($handle); //關(guān)閉指針
- }
- fclose ($fp);
- die ("生成分頁文件完成,如生成不完全,
請(qǐng)檢查文件權(quán)限系統(tǒng)后重新生成!"); - ?>
大致PHP處理分頁的思路如此,其中如其它數(shù)據(jù)生成,數(shù)據(jù)輸入輸出檢查,分頁內(nèi)容指向等可酌情在頁面中加入。