首頁  >  文章  >  後端開發  >  php mysql分頁類別(php新手入門)

php mysql分頁類別(php新手入門)

WBOY
WBOY原創
2016-07-25 09:00:541481瀏覽
複製程式碼
  1. /***

  2. * php mysql 分頁類
  3. * 整理 http://bbs.it-home.org
  4. */
  5. class Pagination{
  6. private $_result;
  7. class Pagination{ private $_count; //記錄數
  8. private $_pageMax; //最大頁
  9. private $_page; //當前頁
  10. private $_url;
  11. private $_startPage;//分頁條起碼
  12. private $_endPage; // 分頁條止碼
  13. private $_nextPage; //上一頁
  14. private $_prePage; //下一頁
  15. function __construct( $table, $pageSize, $getPage){

  16. $this->_result = $GLOBALS['db']->query('SELECT * FROM '.$table);
  17. $this->_count = $ GLOBALS['db']->getRecordNum();
  18. $this->_pageMax = ceil($this->_count/$pageSize);
  19. $this->_result = '';
  20. if($_GET[$getPage] == '')

  21. $this->_page = 1;
  22. else
  23. $this->_page= max(intval($_GET[$ getPage]), 1);
  24. $this->_page = min($this->_page, $this->_pageMax);
  25. $offset = ($this ->_page - 1) * $pageSize;

  26. $sql = 'SELECT * FROM '.$table.' LIMIT '. $offset .','. $pageSize;
  27. $this->_result = $GLOBALSALS ['db']->query($sql);
  28. }
  29. function getRecord(){
  30. return $GLOBALS['db']->getRecord();
  31. }
  32. function getPageBar($url = '?', $barLn = 10, $style = 1){
  33. if($style == 1){
  34. if($barLn % 2 != 0 ){
  35. $midder = ceil($barLn / 2);
  36. $big_repair = $midder - 1 ;//當上面以進一法取整,則這裡為減1,反之為加1
  37. }else{
  38. $midder = $big_repair = $barLn / 2;
  39. }
  40. $sml_repair = $midder- 1;
  41. $this->_startPage = ($this-> _page + $midder) > $this->_pageMax ? $this->_pageMax - $barLn : $this->_page - $sml_repair;
  42. $this->_endPage = $this->_page _page + $big_repair;
  43. }elseif($style == 2){
  44. if($this->_page % $barLn = = 0){
  45. $this->_startPage = $this->_page;
  46. }else{
  47. $this->_startPage = ($this->_page > $barLn)? $this->_page - ($this->_page % $barLn ) : 1;
  48. }
  49. $this->_endPage = $this->_startPage + $barLn - 1;
  50. }
  51. $this->_url = $url;
  52. $this->_nextPage = $this->_page + 1;
  53. $this->_prePage = $this->_page - 1;
  54. $this->_startPage = max($ this->_startPage, 1);//至少從第一頁開始
  55. $this->_endPage = min($this->_endPage, $this->_pageMax);//最多只到末頁
  56. $this->_result = '目前是:'.$this->_page.'/'.$this->_pageMax.'頁,共'.$this->_count.'筆記錄';
  57. if ($this->_page > 1)
  58. $this-> _result .= '
  59. 9
  60. 3
  61. ';
  62. else
  63. $this->_result .= '9
  64. 3';
  65. for($i = $this->_startPage; $i _endPage; $i++) {
  66. if ($this->_page == $i)
  67. $this->_result .= ''.$i.'';
  68. else
  69. $this->_result.= ' '.$i.'';
  70. }
  71. if ($this->_page != $this->_pageMax) {
  72. $this->_result .= '4';
  73. $this-> ;_result .= ' :';
  74. } else {
  75. $this->_result.= '4 :';
  76. }
  77. $this->_result.= '
複製程式碼

2、>複製程式碼

2、呼叫範例
  1. $page = new Pagination($table, 5, 'page');
  2. while($row = $ page ->getRecord()){
  3. echo $row[0],'
    ';
}
echo $page->getPageBar('?',8, 1 ); ?>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn