Maison > développement back-end > tutoriel php > 经典php分页类代码

经典php分页类代码

WBOY
Libérer: 2016-07-25 08:52:35
original
1051 Les gens l'ont consulté
  1. /**************************************
  2. file: class.paging.php
  3. php分页类代码
  4. **************************************/
  5. class paging{
  6. var $sql_results;
  7. var $sql_query;
  8. var $display_offset;
  9. var $display_limit;
  10. var $display_limit_URI;
  11. var $display_currentpage;
  12. var $display_totalpages;
  13. var $alt_content_count;
  14. var $display_offset_var;
  15. var $display_limit_var;
  16. var $display_mid_offset;
  17. var $display_link_first;
  18. var $display_link_prev;
  19. var $display_link_next;
  20. var $display_link_last;
  21. var $page_filename;
  22. var $page_uri;
  23. var $content;
  24. var $content_type;
  25. var $content_count;
  26. ## CONSTRUCTOR
  27. function paging($offsetVar='offset',$limit=10,$midOffset=2){
  28. $this->display_offset = isset($_REQUEST[$offsetVar])?$_REQUEST[$offsetVar]:0;
  29. $this->display_limit = is_numeric($limit)?$limit:10;
  30. $this->display_mid_offset = $midOffset;
  31. $this->display_offset_var = $offsetVar;
  32. $this->display_currentpage= ceil($this->display_offset / $this->display_limit);
  33. }
  34. ## HANDLE DATA
  35. function setContent($content){
  36. if( is_array($content) )
  37. $this->content_type = 'array';
  38. $this->content = $content;
  39. $this->private_contentDetails();
  40. $this->getLinks();
  41. }
  42. function private_contentDetails(){
  43. if( $this->content_type == 'array' )
  44. $this->content_count = count($this->content);
  45. }
  46. function getContent(){
  47. $returnAry = array();
  48. for(
  49. $i=$this->display_offset;
  50. $icontent_count, $this->display_offset+$this->display_limit );
  51. ++$i
  52. )
  53. array_push($returnAry,$this->content[$i]);
  54. return $returnAry;
  55. }
  56. ## LINKS TO NAVIGATE
  57. function getLinks(){
  58. $this->getNextLink('');
  59. $this->getFirstLink('');
  60. $this->getLastLink('');
  61. $this->getPrevLink('');
  62. }
  63. function getNext(){
  64. return $this->display_link_next;
  65. }
  66. function getPrev(){
  67. return $this->display_link_prev;
  68. }
  69. function getFirst(){
  70. return $this->display_link_first;
  71. }
  72. function getLast(){
  73. return $this->display_link_last;
  74. }
  75. function getNextLink($caption){
  76. // Check if we're counting content or the alternate user provided count
  77. if( $this->alt_content_count )
  78. $count = $this->alt_content_count;
  79. else
  80. $count = $this->content_count;
  81. if( $this->display_offset+$this->display_limit-$count >=0 ){
  82. $this->display_link_next = $this->display_offset;
  83. }else{
  84. $this->display_link_next = min(
  85. $count-1,
  86. $this->display_offset+$this->display_limit
  87. );
  88. }
  89. return $this->buildLink( $this->buildNewURI( $this->display_link_next), $caption );
  90. }
  91. function getPrevLink($caption){
  92. $this->display_link_prev = max(
  93. 0,
  94. $this->display_offset-$this->display_limit
  95. );
  96. return $this->buildLink( $this->buildNewURI( $this->display_link_prev), $caption );
  97. }
  98. function getFirstLink($caption){
  99. $this->display_link_first = 0;
  100. return $this->buildLink( $this->buildNewURI( $this->display_link_first), $caption );
  101. }
  102. function getLastLink($caption){
  103. $this->display_link_last = $this->content_count-$this->display_limit;
  104. return $this->buildLink( $this->buildNewURI( $this->display_link_last), $caption );
  105. }
  106. ## NUMBERS
  107. function getPageCount(){
  108. if( $this->alt_content_count )
  109. $count = $this->alt_content_count;
  110. else
  111. $count = $this->content_count;
  112. $this->display_totalpages = ceil(
  113. $count / $this->display_limit
  114. );
  115. return $this->display_totalpages;
  116. }
  117. function getPageNumbers(){
  118. // define variables
  119. $midOffset = $this->display_mid_offset;
  120. $firstLink = $this->display_link_first;
  121. $pageCount = $this->getPageCount();
  122. $thisPage = $this->display_currentpage;
  123. $limit = $this->display_limit;
  124. $displayed=$midOffset*2+1;
  125. $returnAry=array();
  126. // dots
  127. $returnAry['afterdot'] = '';
  128. $returnAry['beforedot'] = '';
  129. // if two times and center the number is less than all the pages together
  130. if( ($midOffset*2+1) $min = max($firstLink,$thisPage-$midOffset);
  131. $max = min($pageCount,$thisPage+$midOffset+1);
  132. $total = $max-$min;
  133. // this keeps x amount of pages displayed even if
  134. // you're not in mid cause of page 1 or 2
  135. if($total
  136. if($min==0){
  137. $max+=$displayed-$total;
  138. }
  139. if($max==$pageCount){
  140. $min-=$displayed-$total;
  141. }
  142. }
  143. }else{
  144. # Just output a set of numbers
  145. $min = 0;
  146. $max = $pageCount;
  147. }
  148. // run pages, check for current page and name it
  149. for($i=$min;$i$cp = 'no';
  150. if($thisPage==$i)
  151. $cp = 'yes';
  152. if($max!=$pageCount)
  153. $returnAry['afterdot'] = '...';
  154. if($min>0)
  155. $returnAry['beforedot'] = '...';
  156. array_push($returnAry,
  157. array(
  158. 'currentPage'=>$cp,
  159. 'pageNumber'=>($i+1),
  160. 'pageLink'=>$this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i+1) )
  161. )
  162. );
  163. }
  164. return $returnAry;
  165. }
  166. function makePageNumbers($format, $pages,$boldCurrent=true,$separator=' '){
  167. $retPages = '';
  168. // make actual page numbers
  169. foreach($pages as $key => $value):
  170. if(is_numeric($key))
  171. $retPages .= ('yes'==$value['currentPage'] && $boldCurrent)?''.$value['pageLink'] .''.$separator:$value['pageLink'].$separator;
  172. endforeach;
  173. $format = str_replace( array('{beforedot}',
  174. '{afterdot}',
  175. '{pages}',
  176. '{separator}'),
  177. array( $pages['beforedot'],
  178. $pages['afterdot'],
  179. $retPages),
  180. $format);
  181. return $format;
  182. }
  183. ## CHECKS
  184. function isFirstPage(){
  185. if($this->display_currentpage==0)
  186. return true;
  187. return false;
  188. }
  189. function isLastPage(){
  190. // add one because basis is 0, not 1
  191. if($this->display_currentpage+1==$this->getPageCount())
  192. return true;
  193. return false;
  194. }
  195. ## FUNCTIONS
  196. function getPageName(){
  197. $fileName = explode('/',$_SERVER['REQUEST_URI']);
  198. $fileName = $fileName[count($fileName)-1];
  199. if(strpos($fileName,'?')>0) {
  200. $fileName = explode('?',$fileName);
  201. $fileName = $fileName[0];
  202. }
  203. return $fileName;
  204. }
  205. function getCleanURI(){
  206. $URI = $_SERVER['REQUEST_URI'];
  207. if(strpos($URI,'?')>0){
  208. $URI = explode('?',$URI);
  209. $URI = $URI[1];
  210. $URI = preg_replace('/\b'.$this->display_offset_var.'\b[=0-9&]{2,20}/','',$URI);
  211. //$URI = preg_replace('/\b'.$this->display_limit_var.'\b[=0-9&]{2,20}/','',$URI);
  212. return $URI;
  213. }
  214. return false;
  215. }
  216. function buildNewURI($offset){
  217. $newFile = $this->getPageName() . '?' . $this->getCleanURI();
  218. $lastChar = substr($newFile,strlen($newFile)-1,1);
  219. if( $lastChar != '&' && $lastChar != '?' )
  220. $newFile.='&';
  221. $newFile .= $this->display_offset_var.'='.$offset.'&';
  222. //$newFile .= $this->display_limit_var.'='.$limit.'&';
  223. return $newFile;
  224. }
  225. function buildLink( $href, $caption, $target=NULL ){
  226. if( $target != NULL )
  227. $target = ' target="'.$target.'"';
  228. return ''.$caption.'';
  229. }
  230. function encodeURI($str){
  231. $salt = 'falaful';
  232. $str = strrev($salt.$str);
  233. return base64_encode($str);
  234. }
  235. function decodeURI($str){
  236. $salt = 'falaful';
  237. $str = strrev( base64_decode($str) );
  238. $str = substr( $str, strlen($salt), strlen($str) );
  239. return $str;
  240. }
  241. ##############
  242. #
  243. #These functions are for inputting a query for this
  244. #class to execute. The other functions will grab all
  245. #x amount of rows then truncate it. These functions will
  246. #only limit to whatever amount. This improves performance.
  247. #Reason is so you dont grab 1,000,000 rows when you want 3.
  248. #
  249. ##############
  250. function runWQuery($db, $statement, $table, $where=''){
  251. // get total rows
  252. $db->query( 'SELECT COUNT(*) AS count FROM '. $table . ' ' . $where );
  253. $db->movenext();
  254. $this->alt_content_count = $db->col['count'];
  255. // add limit to query
  256. $where .= ' LIMIT ' . $this->display_offset .', '.$this->display_limit;
  257. // save query
  258. $this->sql_query = $statement . ' FROM ' . $table .' '. $where;
  259. // print query
  260. //echo $this->sql_query;
  261. // run query
  262. $db->query( $this->sql_query );
  263. $this->sql_results = array();
  264. // save results
  265. while($db->movenext()){
  266. array_push($this->sql_results , $db->col);
  267. }
  268. return $this->runQueryActions();
  269. }
  270. function runQueryActions(){
  271. $this->setContent( $this->sql_results );
  272. $pages = $this->getPageNumbers();
  273. // make actual page numbers
  274. $retPages = $this->makePageNumbers( '{beforedot} {pages} {afterdot}', $pages, true, ' ' );
  275. // get new display
  276. return array(
  277. 'content'=>$this->sql_results,
  278. 'pages'=>$retPages
  279. );
  280. }
  281. }
  282. ?>
复制代码


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal