欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php分页代码实例

程序员文章站 2024-01-29 16:38:16
...
  1. function pagestring($count, $pagesize, $wap=false) {
  2. $p = new Pager($count, $pagesize, 'page');
  3. if ($wap) {
  4. return array($pagesize, $p->offset, $p->genWap());
  5. }
  6. return array($pagesize, $p->offset, $p->genBasic());
  7. }
  8. pagestring文件
  9. php
  10. class Pager{
  11. public $rowCount = 0;
  12. public $pageNo = 1;
  13. public $pageSize = 20;
  14. public $pageCount = 0;
  15. public $offset = 0;
  16. public $pageString = 'page';
  17. private $script = null;
  18. private $valueArray = array();
  19. public function __construct($count=0, $size=20, $string='page')
  20. {
  21. $this->defaultQuery();
  22. $this->pageString = $string;
  23. $this->pageSize = abs($size);
  24. $this->rowCount = abs($count);
  25. $this->pageCount = ceil($this->rowCount/$this->pageSize);
  26. $this->pageCount = ($this->pageCount=0)?1:$this->pageCount;
  27. $this->pageNo = abs(intval(@$_GET[$this->pageString]));
  28. $this->pageNo = $this->pageNo==0 ? 1 : $this->pageNo;
  29. $this->pageNo = $this->pageNo>$this->pageCount
  30. ? $this->pageCount : $this->pageNo;
  31. $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
  32. }
  33. private function genURL( $param, $value ){
  34. $valueArray = $this->valueArray;
  35. $valueArray[$param] = $value;
  36. return $this->script . '?' . http_build_query($valueArray);
  37. }
  38. private function defaultQuery()
  39. {
  40. ($script_uri = @$_SERVER['SCRIPT_URI']) ($script_uri = @$_SERVER['REQUEST_URI']);
  41. $q_pos = strpos($script_uri,'?');
  42. if ( $q_pos > 0 )
  43. {
  44. $qstring = substr($script_uri, $q_pos+1);
  45. parse_str($qstring, $valueArray);
  46. $script = substr($script_uri,0,$q_pos);
  47. }
  48. else
  49. {
  50. $script = $script_uri;
  51. $valueArray = array();
  52. }
  53. $this->valueArray = empty($valueArray) ? array() : $valueArray;
  54. $this->script = $script;
  55. }
  56. public function paginate($switch=1){
  57. $from = $this->pageSize*($this->pageNo-1)+1;
  58. $from = ($from>$this->rowCount) ? $this->rowCount : $from;
  59. $to = $this->pageNo * $this->pageSize;
  60. $to = ($to>$this->rowCount) ? $this->rowCount : $to;
  61. $size = $this->pageSize;
  62. $no = $this->pageNo;
  63. $max = $this->pageCount;
  64. $total = $this->rowCount;
  65. return array(
  66. 'offset' => $this->offset,
  67. 'from' => $from,
  68. 'to' => $to,
  69. 'size' => $size,
  70. 'no' => $no,
  71. 'max' => $max,
  72. 'total' => $total,
  73. );
  74. }
  75. public function GenWap() {
  76. $r = $this->paginate();
  77. $pagestring= '

    '

    ;
  78. if( $this->pageNo > 1 ){
  79. $pageString.= '4 >genURL($this->pageString, $this->pageNo-1) . '" accesskey="4">上页a>';
  80. }
  81. if( $this->pageNo >1 && $this->pageNo $this->pageCount ){
  82. $pageString.= '|';
  83. }
  84. if( $this->pageNo $this->pageCount ) {
  85. $pageString.= '>genURL($this->pageString, $this->pageNo+1) . '" accesskey="6">下页a> 6';
  86. }
  87. $pageString.= '';
  88. return $pageString;
  89. }
  90. public function GenBasic() {
  91. $r = $this->paginate();
  92. $buffer = null;
  93. $index = '首页';
  94. $pre = '上一页';
  95. $next = '下一页';
  96. $last = '末页';
  97. if ($this->pageCount=7) {
  98. $rangerange = range(1,$this->pageCount);
  99. } else {
  100. $min = $this->pageNo - 3;
  101. $max = $this->pageNo + 3;
  102. if ($min 1) {
  103. $max += (3-$min);
  104. $min = 1;
  105. }
  106. if ( $max > $this->pageCount ) {
  107. $min -= ( $max - $this->pageCount );
  108. $max = $this->pageCount;
  109. }
  110. $min = ($min>1) ? $min : 1;
  111. $rangerange = range($min, $max);
  112. }
  113. $buffer .= '
      '
    ;
  114. $buffer .= "
  115. ({$this->rowCount})
  116. ";
  117. if ($this->pageNo > 1) {
  118. $buffer .= "
  119. >{$index}a>li>a href='http://www.cxybl.com/".$this->genURL($this->pageString,$this->pageNo-1)."'>{$pre}a>";
  120. }
  121. foreach($range AS $one) {
  122. if ( $one == $this->pageNo ) {
  123. $buffer .= "li class=\"current\">{$one}li>";
  124. } else {
  125. $buffer .= "
  126. >{$one}a>li>";
  127. }
  128. }
  129. if ($this->pageNo $this->pageCount) {
  130. $buffer .= "
  131. >{$next}a>li>li>a href='http://www.cxybl.com/".$this->genURL($this->pageString, $this->pageCount)."'>{$last}a>li>";
  132. }
  133. return $buffer . 'ul>';
  134. }
  135. }
  136. ?>