php mysql分页类(php新手入门)

WBOY
Lepaskan: 2016-07-25 09:00:54
asal
1460 orang telah melayarinya
  1. /***

  2. * php mysql 分页类
  3. * 整理 http://bbs.it-home.org
  4. */
  5. class Pagination{
  6. private $_result;
  7. 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 = $GLOBALS['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.= '
  78. ';
  79. return $this->_result;
  80. }
  81. }
  82. ?>
复制代码

2、调用示例

  1. $page = new Pagination($table, 5, 'page');
  2. while($row = $page->getRecord()){
  3. echo $row[0],'
    ';
  4. }
  5. echo $page->getPageBar('?',8, 1);
  6. ?>
复制代码


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!