Home > Backend Development > PHP Tutorial > PHP paging code, supports multiple styles and can set the paging method

PHP paging code, supports multiple styles and can set the paging method

WBOY
Release: 2016-07-25 08:52:39
Original
1051 people have browsed it
  1. class Page {

  2. private $total;//Total quantity
  3. private $limit;//Return the limit statement of mysql
  4. private $pageStart;//Starting value
  5. private $pageStop;//Ending value
  6. private $pageNumber;//Display the number of paging numbers
  7. private $page;//Current page
  8. private $pageSize;//The number displayed on each page
  9. private $pageToatl;//Paging The total number of
  10. private $pageParam;//Paging variables
  11. private $uri;//URL parameters
  12. /**
  13. * The paging setting style is not case-sensitive
  14. * %pageToatl% //Total number of pages
  15. * %page%//Current page
  16. * %pageSize% //Number of data items displayed on the current page
  17. * %pageStart%//This page Starting number
  18. * %pageStop%//Ending number of this page
  19. * %total%//Total number of data
  20. * %first%//Homepage
  21. * %ending%//Last page
  22. * %up%/ /Previous page
  23. * %down%//Next page
  24. * %F%//Start page
  25. * %E%//End page
  26. * %omitFA%//Omit and jump before
  27. * %omitEA% //Omit after and jump
  28. * %omitF%//Omit before
  29. * %omitE%//Omit after
  30. * %numberF%//Fixed number of number pagination
  31. * %numberD%//Equal left and right number pagination
  32. * %input%//Jump input box
  33. * %GoTo%//Jump button
  34. */ bbs.it-home.org
  35. private $pageType = 'Page %page%/Total%pageToatl%%first%%up%%F%%omitFA%%numberF%%omitEA%%E%%down%%ending%';
  36. / /Display value setting
  37. private $pageShow = array('first'=>'Home page','ending'=>'Last page','up'=>'Previous page','down'=> 'Next page','GoTo'=>'GO');

  38. /**

  39. * Initialization data, construction method
  40. * @access public
  41. * @param int $total The total number of data
  42. * @param int $pageSize The number of items displayed on each page
  43. * @param int $pageNumber The number of paging numbers displayed (use %numberF% It will have different effects than using %numberD%)
  44. * @param string $pageParam paging variable
  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. * Show pagination
  56. * @access public
  57. * @return string HTML pagination string
  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 = ''.$this->pageShow['first'].'';
  67. $up = ''.$this->pageShow['up'] .'';
  68. }
  69. if($this->page >= $this->pageToatl){
  70. $ending = ''.$this ->pageShow['ending'].'';
  71. $down = ''.$this->pageShow['down'].'< /span>';
  72. }else{
  73. $ending = ''.$this->pageShow['ending'].'';
  74. $down = ''.$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. *Number Pagination
  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 .= ''.$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 .= ''.$i.'';
  117. }
  118. $numberD .= ''.$this->page.'';
  119. $start = $this->page+1;
  120. for($i=$start;$i<$forStop;++$i){
  121. $numberD .= ''.$i.'';
  122. }
  123. }
  124. $F = $forStart>1?'1':'';
  125. $E = $forStop<$this->pageToatl+1?''.$this->pageToatl.'':'';
  126. if($forStart>2){
  127. $omitF = '';
  128. $startA = $this->page-$number<1?1:$this->page-$number;
  129. $omitFA = '';
  130. }
  131. if($forStop<$this->pageToatl){
  132. $omitE = '';
  133. $stopA = $this->page+$number>$this->pageToatl?$this->pageToatl:$this->page+$number;
  134. $omitEA = '';
  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. *Methods for processing url
  141. * @access public
  142. * @param array $url Keep the URL directly in the relationship array
  143. * @return string filtered url tail parameters
  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. * Set the limit method and calculate the starting number and ending number
  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. * Set filters
  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. * Value filter
  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. }

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template