当前位置:Gxlcms > PHP教程 > php分页类代码,支持多样式可设置分页方式

php分页类代码,支持多样式可设置分页方式

时间:2021-07-01 10:21:17 帮助过:27人阅读

  1. class Page {

  2. private $total;//总数量
  3. private $limit;//返回mysql的limit语句
  4. private $pageStart;//开始的数值
  5. private $pageStop;//结束的数值
  6. private $pageNumber;//显示分页数字的数量
  7. private $page;//当前页
  8. private $pageSize;//每页显示的数量
  9. private $pageToatl;//分页的总数量
  10. private $pageParam;//分页变量
  11. private $uri;//URL参数
  12. /**
  13. * 分页设置样式 不区分大小写
  14. * %pageToatl% //总页数
  15. * %page%//当前页
  16. * %pageSize% //当前页显示数据条数
  17. * %pageStart%//本页起始条数
  18. * %pageStop%//本页结束条数
  19. * %total%//总数据条数
  20. * %first%//首页
  21. * %ending%//尾页
  22. * %up%//上一页
  23. * %down%//下一页
  24. * %F%//起始页
  25. * %E%//结束页
  26. * %omitFA%//前省略加跳转
  27. * %omitEA%//后省略加跳转
  28. * %omitF%//前省略
  29. * %omitE%//后省略
  30. * %numberF%//固定数量的数字分页
  31. * %numberD%//左右对等的数字分页
  32. * %input%//跳转输入框
  33. * %GoTo%//跳转按钮
  34. */ bbs.it-home.org
  35. private $pageType = '第%page%页/共%pageToatl%页%first%%up%%F%%omitFA%%numberF%%omitEA%%E%%down%%ending%';
  36. //显示值设置
  37. private $pageShow = array('first'=>'首页','ending'=>'尾页','up'=>'上一页','down'=>'下一页','GoTo'=>'GO');

  38. /**

  39. * 初始化数据,构造方法
  40. * @access public
  41. * @param int $total 数据总数量
  42. * @param int $pageSize 每页显示条数
  43. * @param int $pageNumber 分页数字显示数量(使用%numberF%和使用%numberD%会有不同的效果)
  44. * @param string $pageParam分页变量
  45. * @return void
  46. */
  47. public function __construct($total,$pageSize=10,$pageNumber=5,$pageParam='p'){
  48. $this->total = $total<0?0:$total;
  49. $this->pageSize = $pageSize<0?0:$pageSize;
  50. $this->pageNumber = $pageNumber<0?0:$pageNumber;
  51. $this->pageParam = $pageParam;
  52. $this->calculate();
  53. }

  54. /**

  55. * 显示分页
  56. * @access public
  57. * @return string HTML分页字符串
  58. */
  59. public function pageShow(){
  60. $this->uri();
  61. if($this->pageToatl>1){
  62. if($this->page == 1){
  63. $first = ''.$this->pageShow['first'].'';
  64. $up = ''.$this->pageShow['up'].'';
  65. }else{
  66. $first = 'uri.'&'.$this->pageParam.'=1">'.$this->pageShow['first'].'';
  67. $up = 'uri.'&'.$this->pageParam.'='.($this->page-1).'">'.$this->pageShow['up'].'';
  68. }
  69. if($this->page >= $this->pageToatl){
  70. $ending = ''.$this->pageShow['ending'].'';
  71. $down = ''.$this->pageShow['down'].'';
  72. }else{
  73. $ending = 'uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageShow['ending'].'';
  74. $down = 'uri.'&'.$this->pageParam.'='.($this->page+1).'">'.$this->pageShow['down'].'';
  75. }
  76. $input = '';
  77. $GoTo = '';
  78. }else{
  79. $first = '';$up ='';$ending = '';$down = '';$input = '';$GoTo = '';
  80. }
  81. $this->numberF();
  82. return str_ireplace(array('%pageToatl%','%page%','%pageSize%','%pageStart%','%pageStop%','%total%','%first%','%ending%','%up%','%down%','%input%','%GoTo%'),array($this->pageToatl,$this->page,$this->pageStop-$this->pageStart,$this->pageStart,$this->pageStop,$this->total,$first,$ending,$up,$down,$input,$GoTo),$this->pageType);
  83. }

  84. /**

  85. *数字分页
  86. */
  87. private function numberF(){
  88. $pageF = stripos($this->pageType,'%numberF%');
  89. $pageD = stripos($this->pageType,'%numberD%');
  90. $numberF = '';$numberD = '';$F = '';$E ='';$omitF = '';$omitFA = '';$omitE = '';$omitEA = '';
  91. if($pageF!==false || $pageD!==false){
  92. if($pageF!==false){
  93. $number = $this->pageNumber%2==0?$this->pageNumber/2:($this->pageNumber+1)/2;
  94. $DStart = $this->page - $number<0?$this->page - $number-1:0;
  95. if($this->page+$number-$DStart > $this->pageToatl){
  96. $DStop = ($this->page+$number-$DStart) - $this->pageToatl;
  97. $forStop = $this->pageToatl+1;
  98. }else{
  99. $DStop = 0;
  100. $forStop = $this->page+$number-$DStart+1;
  101. }
  102. $forStart = $this->page-$number-$DStop<1?1:$this->page-$number-$DStop;
  103. for($i=$forStart;$i<$forStop;++$i){
  104. if($i==$this->page){
  105. $numberF .= ''.$i.'';
  106. }else{
  107. $numberF .= 'uri.'&'.$this->pageParam.'='.$i.'">'.$i.'';
  108. }
  109. }
  110. }
  111. if($pageD!==false){
  112. $number = $this->pageNumber;
  113. $forStart = $this->page-$number>0?$this->page-$number:1;
  114. $forStop = $this->page+$number>$this->pageToatl?$this->pageToatl+1:$this->page+$number+1;
  115. for($i=$forStart;$i<$this->page;++$i){
  116. $numberD .= 'uri.'&'.$this->pageParam.'='.$i.'">'.$i.'';
  117. }
  118. $numberD .= ''.$this->page.'';
  119. $start = $this->page+1;
  120. for($i=$start;$i<$forStop;++$i){
  121. $numberD .= 'uri.'&'.$this->pageParam.'='.$i.'">'.$i.'';
  122. }
  123. }
  124. $F = $forStart>1?'uri.'&'.$this->pageParam.'=1">1':'';
  125. $E = $forStop<$this->pageToatl+1?'uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageToatl.'':'';
  126. if($forStart>2){
  127. $omitF = '';
  128. $startA = $this->page-$number<1?1:$this->page-$number;
  129. $omitFA = 'uri.'&'.$this->pageParam.'='.$startA.'">…';
  130. }
  131. if($forStop<$this->pageToatl){
  132. $omitE = '';
  133. $stopA = $this->page+$number>$this->pageToatl?$this->pageToatl:$this->page+$number;
  134. $omitEA = 'uri.'&'.$this->pageParam.'='.$stopA.'">…';
  135. }
  136. }
  137. $this->pageType = str_ireplace(array('%F%','%E%','%omitFA%','%omitEA%','%omitF%','%omitE%','%numberF%','%numberD%'),array($F,$E,$omitFA,$omitEA,$omitF,$omitE,$numberF,$numberD),$this->pageType);
  138. }

  139. /**

  140. *处理url的方法
  141. * @access public
  142. * @param array $url 保持URL直对关系数组
  143. * @return string 过滤后的url尾参数
  144. */
  145. private function uri(){
  146. $url = $_SERVER["REQUEST_URI"];
  147. $par = parse_url($url);
  148. if (isset($par['query'])) {
  149. parse_str($par['query'],$query);
  150. if(!is_array($this->uri)){
  151. $this->uri = array();
  152. }
  153. array_merge($query,$this->uri);
  154. unset($query[$this->pageParam]);
  155. while($key = array_search('',$query)){
  156. unset($query[$key]);
  157. }
  158. $this->uri = $par['path'].'?'.http_build_query($query);
  159. }else{
  160. $this->uri = $par['path'].'?';
  161. }
  162. }

  163. /**

  164. * 设置limit方法及计算起始条数和结束条数
  165. */
  166. private function calculate(){
  167. $this->pageToatl = ceil($this->total/$this->pageSize);
  168. $this->page = intval($_GET[$this->pageParam]);
  169. $this->page = $this->page>=1?$this->page>$this->pageToatl?$this->pageToatl:$this->page:1;
  170. $this->pageStart = ($this->page-1)*$this->pageSize;
  171. $this->pageStop = $this->pageStart+$this->pageSize;
  172. $this->pageStop = $this->pageStop>$this->total?$this->total:$this->pageStop;
  173. $this->limit = $this->pageStart.','.$this->pageStop;
  174. }

  175. /**

  176. * 设置过滤器
  177. */
  178. public function __set($name,$value){
  179. switch($name){
  180. case 'pageType':
  181. case 'uri':
  182. $this->$name = $value;
  183. return;
  184. case 'pageShow':
  185. if(is_array($value)){
  186. $this->pageShow = array_merge($this->pageShow,$value);
  187. }
  188. return;
  189. }
  190. }

  191. /**

  192. * 取值过滤器
  193. */
  194. public function __get($name){
  195. switch($name){
  196. case 'limit':
  197. case 'pageStart':
  198. case 'pageStop':
  199. return $this->$name;
  200. default:
  201. return;
  202. }
  203. }
  204. }

人气教程排行