你不可錯過的一個php分頁類別(mysql)

WBOY
發布: 2016-07-25 09:05:19
原創
670 人瀏覽過
  1. /*

  2. mysql_pager.class.php
  3. 三個參數:mysql_query()的結果, url變數page, 您想要的每頁記錄數
  4. */
  5. class mysql_pager {

  6. // define properties
  7. var $page;
  8. var $result;
  9. var $results_per_page = 3;
  10. var $total_pages;
  11. /*

  12. Define the methods
  13. 下面是建構函數,和類別同名(>php4),需要查詢的結果句柄,目前頁碼,每頁記錄數

  14. like: $f->mysql_pager($result, 1, 15);
  15. */
  16. function mysql_pager( $result, $current_page, $results_per_page ) {
  17. if(!$result){
  18. echo "
    資料庫未執行,結果集錯誤
    n";
  19. return;
  20. }
  21. $this->result = $result;

  22. if(!$current_page || $current_page $this ->page = 1;

  23. else $this->page = $current_page;
  24. if(!emptyempty($results_per_page))

  25. $this->results_per_page = $results_per_page;
  26. $numrows = @mysql_num_rows($this->result);

  27. if(!$numrows) {
  28. echo "
    查詢結果為空.n";
  29. return;
  30. }
  31. $this->total_pages = ceil($numrows / $this->results_per_page);

  32. }
  33. /*

  34. 下面是列印內容的函數,可以不用,也可以依照自己的需求擴充
  35. 這裡只是印出id
  36. */
  37. function print_paged_results() {
  38. echo "n";
  39. $start = ($this->page - 1) * $this->results_per_page;
  40. mysql_data_seek($this->result, $start);
  41. $x = 0;
  42. for($i = 1; $i results_per_page && $row = @mysql_fetch_array($this->result); $i++) {
  43. if($x++ & 1) $bgcolor = "#F2F2FF";
  44. else $bgcolor = "#EEEEEE";
  45. echo "

  46. ";
  47. // 編輯這部分輸出任何您想要的HTML
  48. }
  49. echo "
  50. ". $row["id"] . "
    n";
  51. }
  52. /*

  53. 下面是列印頁碼和連結的函數,在需要顯示頁碼的地方呼叫
  54. */
  55. function print_navigation() {
  56. global $PHP_SELF;
  57. echo "
    ";
  58. for($i = 1; $i total_pages; $i++) { #loop to print >
  59. if($i == 1 && $this->page > 1) #Prints the echo "page - 1)."" onMouseOver="status="Previous Page";return true;" onMouseOut="status=" ";return true ;">?";
  60. if($i == $this->page) #Doesn"t print a link itself, just prints page number

  61. echo " $i ";
  62. if($i != $this->page) #Other links that aren"t this page go here

  63. echo " $i ";
  64. if($i == $this->total_pages && $this->page != $this->total_pages) # Link for next page >> (not on last page)

  65. echo "page + 1)."" onMouseOver="status="Go to the Next Page";return true;" onMouseOut="status=" ";return true;">?";
  66. }
  67. echo "

n";
  • }
  • }
  • /* 範例http://bbs.it-home.org

  • mysql_connect($server, $uname, $pass );
  • mysql_select_db("$db") ;
  • $result= @mysql_query("Select * FROM table");
  • $p = new mysql_pager( $result, $page=$_GET["page"], 10 ) ;

  • $p->print_navigation();
  • $p->print_paged_results();
  • $p->print_navigation();
  • */
  • ?>
  • 複製程式碼


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