一款php分页代码

以前写过很多php 分页类但是今天这款分页程序我感觉是很好的,简洁实用,代码合理并没有多余的代码,是一款不错分页类函数哦。

  1. class multipage {
  2. var $total;
  3. var $perpage;
  4. var $pages;
  5. var $maxpage;
  6. var $offset = 9;
  7. var $curr_page;
  8. function init($total, $perpage, $maxpage) { //初始化页数
  9. $this->total;
  10. $this->perpage;
  11. $this->maxpage;
  12. $this->offset = 9;
  13. }
  14. function getpagelist() {//获取分页列表
  15. $result_pages = "";
  16. $this->pages = ceil($this->total / $this->perpage);
  17. if ($this->pages > $this->maxpage) {
  18. $from = $this->curr_page - $this->offset;
  19. if ($from < 1) {
  20. $from = 1;
  21. }
  22. $to = $from + $this->maxpage - 1;
  23. if ($to > $this->pages) {
  24. $to = $this->pages;
  25. if (($to - $from) < $this->maxpage) {
  26. $from = $from - 1;
  27. }
  28. }
  29. } else {
  30. $from = 1;
  31. $to = $this->pages;
  32. }
  33. $p = 0;
  34. for($i = $from; $i <= $to; $i++) {
  35. $result_pages[$p] = $i;
  36. $p++;
  37. }
  38. return $result_pages;
  39. }
  40. function getfirst() { //获取第一页
  41. if ($this->curr_page > 1 && $this->pages > 1) {
  42. return 1;
  43. } else {
  44. return "";
  45. }
  46. }
  47. function getlast() { //取末页
  48. if ($this->pages > 1 && $this->curr_page < $this->pages) {
  49. return $this->pages;
  50. } else {
  51. return "";
  52. }
  53. }
  54. function getprev() {//上一页
  55. $prevpage = $this->curr_page - 1;
  56. if ($prevpage > 0) {
  57. return $prevpage;
  58. } else {
  59. $prevpage = "";
  60. return $prevpage;
  61. }
  62. }
  63. function getnext() {//下一页
  64. $nextpage = $this->curr_page + 1;
  65. if ($nextpage <= $this->pages) {
  66. return $nextpage;
  67. } else {
  68. $nextpage = "";
  69. return $nextpage;
  70. }
  71. }
  72. function gettotal() {//共多少页
  73. if ($this->pages > 0) {
  74. return $this->pages;
  75. } else {
  76. return 1;
  77. }
  78. }
  79. }
  80. //分页类的使用方法
  81. $page = new multipage();
  82. $page->gettotal(); //总页娄
  83. $page->getnext();//下一页