使用相關函數實現PHP處理分頁_PHP教程

來源:互聯網
上載者:User
通過對

如我們指定分頁時,每頁20篇。某子頻道列表內文章經資料庫查詢為45條,則,首先我們通過查詢得到如下參數:1,總頁數;2,每頁篇數。

PHP處理分頁第二 步,for ($i = 0; $i < allpages; $i++),頁面元素擷取,分析,文章產生,都在此迴圈中執行。不同的是,die ("建立檔案".$filename."成功!";這句去掉,放到迴圈後的顯示,因為該語句將中止程式執行。例:

  1. < ?php
  2. $fp = fopen ("temp.html","r");
  3. $content = fread ($fp,filesize ("temp.html"));
  4. $onepage = '20';
  5. $sql = "select id from article where
    channel='$channelid'"
    ;
  6. $query = mysql_query ($sql);
  7. $num = mysql_num_rows ($query);
  8. $allpages = ceil ($num / $onepage);
  9. for ($i = 0;$i<$allpages; $i++){
  10. if ($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 .= '.$root.$result['filename']
    .'
    target=_blank>'.$title.'a><br>';
  21. }
  22. $content = str_replace ("{articletable}
    ",$list,$content);
  23. if (is_file ($indexpath)){
  24. @unlink ($indexpath); //若檔案已存在,則刪除
  25. }
  26. $handle = fopen ($indexpath,"w");
    //開啟檔案指標,建立檔案
  27. /*
  28. 檢查檔案是否被建立且可寫
  29. */
  30. if (!is_writable ($indexpath)){
  31. echo "檔案:".$indexpath."不可寫,
    請檢查其屬性後重試!"; //修改為echo
  32. }
  33. if (!fwrite ($handle,$content)){ //將資訊寫入檔案
  34. echo "組建檔案".$indexpath."失敗!"; //修改為echo
  35. }
  36. fclose ($handle); //關閉指標
  37. }
  38. fclose ($fp);
  39. die ("產生分頁檔案完成,如產生不完全,
    請檢查檔案許可權系統後重建!");
  40. ?>

大致PHP處理分頁的思路如此,其中如其它資料產生,資料輸入輸出檢查,分頁內容指向等可酌情在頁面中加入。


http://www.bkjia.com/PHPjc/445950.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445950.htmlTechArticle通過對 如我們指定分頁時,每頁20篇。某子頻道列表內文章經資料庫查詢為45條,則,首先我們通過查詢得到如下參數:1,總頁數;2,每頁...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.