当前位置:Gxlcms > PHP教程 > 经典php分页类代码

经典php分页类代码

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

  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. $i< min( $this->content_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) < $pageCount ){
  131. $min = max($firstLink,$thisPage-$midOffset);
  132. $max = min($pageCount,$thisPage+$midOffset+1);
  133. $total = $max-$min;
  134. // this keeps x amount of pages displayed even if
  135. // you're not in mid cause of page 1 or 2
  136. if($total<$displayed){
  137. if($min==0){
  138. $max+=$displayed-$total;
  139. }
  140. if($max==$pageCount){
  141. $min-=$displayed-$total;
  142. }
  143. }
  144. }else{
  145. # Just output a set of numbers
  146. $min = 0;
  147. $max = $pageCount;
  148. }
  149. // run pages, check for current page and name it
  150. for($i=$min;$i<$max;++$i){
  151. $cp = 'no';
  152. if($thisPage==$i)
  153. $cp = 'yes';
  154. if($max!=$pageCount)
  155. $returnAry['afterdot'] = '...';
  156. if($min>0)
  157. $returnAry['beforedot'] = '...';
  158. array_push($returnAry,
  159. array(
  160. 'currentPage'=>$cp,
  161. 'pageNumber'=>($i+1),
  162. 'pageLink'=>$this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i+1) )
  163. )
  164. );
  165. }
  166. return $returnAry;
  167. }
  168. function makePageNumbers($format, $pages,$boldCurrent=true,$separator=' '){
  169. $retPages = '';
  170. // make actual page numbers
  171. foreach($pages as $key => $value):
  172. if(is_numeric($key))
  173. $retPages .= ('yes'==$value['currentPage'] && $boldCurrent)?''.$value['pageLink'] .''.$separator:$value['pageLink'].$separator;
  174. endforeach;
  175. $format = str_replace( array('{beforedot}',
  176. '{afterdot}',
  177. '{pages}',
  178. '{separator}'),
  179. array( $pages['beforedot'],
  180. $pages['afterdot'],
  181. $retPages),
  182. $format);
  183. return $format;
  184. }
  185. ## CHECKS
  186. function isFirstPage(){
  187. if($this->display_currentpage==0)
  188. return true;
  189. return false;
  190. }
  191. function isLastPage(){
  192. // add one because basis is 0, not 1
  193. if($this->display_currentpage+1==$this->getPageCount())
  194. return true;
  195. return false;
  196. }
  197. ## FUNCTIONS
  198. function getPageName(){
  199. $fileName = explode('/',$_SERVER['REQUEST_URI']);
  200. $fileName = $fileName[count($fileName)-1];
  201. if(strpos($fileName,'?')>0) {
  202. $fileName = explode('?',$fileName);
  203. $fileName = $fileName[0];
  204. }
  205. return $fileName;
  206. }
  207. function getCleanURI(){
  208. $URI = $_SERVER['REQUEST_URI'];
  209. if(strpos($URI,'?')>0){
  210. $URI = explode('?',$URI);
  211. $URI = $URI[1];
  212. $URI = preg_replace('/\b'.$this->display_offset_var.'\b[=0-9&]{2,20}/','',$URI);
  213. //$URI = preg_replace('/\b'.$this->display_limit_var.'\b[=0-9&]{2,20}/','',$URI);
  214. return $URI;
  215. }
  216. return false;
  217. }
  218. function buildNewURI($offset){
  219. $newFile = $this->getPageName() . '?' . $this->getCleanURI();
  220. $lastChar = substr($newFile,strlen($newFile)-1,1);
  221. if( $lastChar != '&' && $lastChar != '?' )
  222. $newFile.='&';
  223. $newFile .= $this->display_offset_var.'='.$offset.'&';
  224. //$newFile .= $this->display_limit_var.'='.$limit.'&';
  225. return $newFile;
  226. }
  227. function buildLink( $href, $caption, $target=NULL ){
  228. if( $target != NULL )
  229. $target = ' target="'.$target.'"';
  230. return ''.$caption.'';
  231. }
  232. function encodeURI($str){
  233. $salt = 'falaful';
  234. $str = strrev($salt.$str);
  235. return base64_encode($str);
  236. }
  237. function decodeURI($str){
  238. $salt = 'falaful';
  239. $str = strrev( base64_decode($str) );
  240. $str = substr( $str, strlen($salt), strlen($str) );
  241. return $str;
  242. }
  243. ##############
  244. #
  245. #These functions are for inputting a query for this
  246. #class to execute. The other functions will grab all
  247. #x amount of rows then truncate it. These functions will
  248. #only limit to whatever amount. This improves performance.
  249. #Reason is so you dont grab 1,000,000 rows when you want 3.
  250. #
  251. ##############
  252. function runWQuery($db, $statement, $table, $where=''){
  253. // get total rows
  254. $db->query( 'SELECT COUNT(*) AS count FROM '. $table . ' ' . $where );
  255. $db->movenext();
  256. $this->alt_content_count = $db->col['count'];
  257. // add limit to query
  258. $where .= ' LIMIT ' . $this->display_offset .', '.$this->display_limit;
  259. // save query
  260. $this->sql_query = $statement . ' FROM ' . $table .' '. $where;
  261. // print query
  262. //echo $this->sql_query;
  263. // run query
  264. $db->query( $this->sql_query );
  265. $this->sql_results = array();
  266. // save results
  267. while($db->movenext()){
  268. array_push($this->sql_results , $db->col);
  269. }
  270. return $this->runQueryActions();
  271. }
  272. function runQueryActions(){
  273. $this->setContent( $this->sql_results );
  274. $pages = $this->getPageNumbers();
  275. // make actual page numbers
  276. $retPages = $this->makePageNumbers( '{beforedot} {pages} {afterdot}', $pages, true, ' ' );
  277. // get new display
  278. return array(
  279. 'content'=>$this->sql_results,
  280. 'pages'=>$retPages
  281. );
  282. }
  283. }
  284. ?>

人气教程排行