首页 >后端开发 >php教程 >php分页类代码,支持多样式可设置分页方式

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

WBOY
WBOY原创
2016-07-25 08:52:391061浏览
  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$this->pageSize = $pageSize$this->pageNumber = $pageNumber$this->pageParam = $pageParam;
  49. $this->calculate();
  50. }
  51. /**

  52. * 显示分页
  53. * @access public
  54. * @return string HTML分页字符串
  55. */
  56. public function pageShow(){
  57. $this->uri();
  58. if($this->pageToatl>1){
  59. if($this->page == 1){
  60. $first = ''.$this->pageShow['first'].'';
  61. $up = ''.$this->pageShow['up'].'';
  62. }else{
  63. $first = ''.$this->pageShow['first'].'';
  64. $up = ''.$this->pageShow['up'].'';
  65. }
  66. if($this->page >= $this->pageToatl){
  67. $ending = ''.$this->pageShow['ending'].'';
  68. $down = ''.$this->pageShow['down'].'';
  69. }else{
  70. $ending = ''.$this->pageShow['ending'].'';
  71. $down = ''.$this->pageShow['down'].'';
  72. }
  73. $input = '';
  74. $GoTo = '';
  75. }else{
  76. $first = '';$up ='';$ending = '';$down = '';$input = '';$GoTo = '';
  77. }
  78. $this->numberF();
  79. 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);
  80. }
  81. /**

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

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

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

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

  187. * 取值过滤器
  188. */
  189. public function __get($name){
  190. switch($name){
  191. case 'limit':
  192. case 'pageStart':
  193. case 'pageStop':
  194. return $this->$name;
  195. default:
  196. return;
  197. }
  198. }
  199. }
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn