經典php分頁類別程式碼

WBOY
發布: 2016-07-25 08:52:35
原創
1011 人瀏覽過
  1. /**************************************
  2. file: class.paging.php
  3. php分頁類別代碼
  4. **************************************/
  5. 類別分頁{
  6. var $sql_results;
  7. var $🎜>var $ sql_query;
  8. var $display_offset;
  9. var $display_limit;
  10. var $display_limit_URI;
  11. var $display_currentpage;
  12. var $display__o_f;
  13. var $display_offset_var;
  14. var $display_limit_var;
  15. var $display_mid_offset;
  16. var $display_link_first;
  17. var $display_olink_o_link_link_link_nlink_ovar display_link_last;
  18. var $page_filename;
  19. var $page_uri;
  20. var $content;
  21. var $content_type;
  22. var $content_count #
  23. #🎜> # 埃國定利
  24. 定利分頁($offsetVar='offset',$limit=10,$midOffset=2){
  25. $this->display_offset = isset($_REQUEST[$offsetVar])?$_REQUESToffset = isset($_REQUEST[$offsetVar])?$_REQUEST [ $ offsetVar]:0;
  26. $this->display_limit = is_numeric($limit)?$limit:10;
  27. $this->display_mid_offset = $midOffset;
  28. $this->display_offset_var = $offsetVar ;
  29. $this->display_currentpage= ceil($this->display_offset / $this->display_limit);
  30. }
  31. ## 電視安全於
  32. function setContent( $content){
  33. if( is_array($content) )
  34. $this->content_type = 'array';
  35. $this->content = $content;
  36. $this ->private_contentDetails();
  37. $this->getLinks();
  38. }
  39. function private_contentDetails(){
  40. if( $this->content_type == 'array' )
  41. $this->content_count$this->content_count = count( $this->content);
  42. }
  43. function getContent(){
  44. $returnAry = array();
  45. for (
  46. $i=$this->顯示偏移量;
  47. $icontent_count, $this->display_offset $this->display_limit );
  48. $i
  49. )
  50. array_push($returnAry,$this->content[$ i] );
  51. return $returnAry;
  52. }
  53. ## 導航連結
  54. function getLinks(){
  55. $this->getNextLink('');
  56. $this->getFirstLink('');
  57. $this->getLastLink('');
  58. $this->getPrevLink('');
  59. }
  60. function getNext (){
  61. return $this->display_link_next;
  62. }
  63. function getPrev(){
  64. return $this->display_link_prev;}
  65. return $this->display_link_prev;}
  66. (){
  67. return $this->display_link_first;
  68. }
  69. function getLast(){
  70. return $this->display_link_last;
  71. }
  72. return $this->display_link_last;
  73. }
  74. ($caption){
  75. // 手機我們可以方法內容或備用電影電影內容
  76. if( $this->alt_content_count )
  77. $count = $this ->alt_content_count;
  78. else
  79. $count = $this->content_count;
  80. if( $this->display_offset $this->display_limit-$count >=0 ) {
  81. $this->display_link_next = $ this - >display_offset;
  82. }else{
  83. $this->display_link_next = min(
  84. $count-1,
  85. $this->display_offset $this->display_limit
  86. );
  87. }
  88. }
  89. return $this->buildLink( $this->buildNewURI( $this->display_link_next), $caption ) ;
  90. }
  91. function getPrevLink($caption){
  92. function getPrevLink($caption){
  93. $this - >display_link_prev = max(
  94. 0,
  95. $this->display_offset-$this->; display_limit
  96. );
  97. return $this->buildLink( $this->buildNewURI( $this->display_link_prev), $caption );
  98. }
  99. ftion getFirstLink($unction)
  100. $this->display_link_first = 0;
  101. return $this->buildLink( $this->buildNewURI( $this->display_link_first), $caption );
  102. }
  103. $this->display_link_last = $this->content_count-$this->display_limit;
  104. return $this->buildLink( $this->buildNewURI( $this->display_link_last), $標題);
  105. }
  106. ## NUMBERS
  107. function getPageCount(){
  108. if( $this->alt_content_count )
  109. $count = $this->alt_content_count; else
  110. $count = $this->content_count;
  111. $this->display_totalpages = ceil(
  112. $count / $this->display_limit
  113. );
  114. return $this- >display_totalpages;
  115. }
  116. function getPageNumbers(){
  117. //定義變數
  118. $midOffset = $this->display_mid_offset;
  119. $firstLink = $this-displaysplay_link_first;
  120. $firstLink = $this-display_link_first; = $this->getPageCount();
  121. $thisPage = $this->display_currentpage;
  122. $limit = $this->display_limit;
  123. $displayed=$midOffset*2 1;
  124. $returnAry =array();
  125. // 點
  126. $returnAry['afterdot'] = '';
  127. $returnAry['beforedot'] = '';
  128. // 如果兩次並居中,則數量小於所有頁面的總和
  129. if( ($midOffset*2 1) $min = max($firstLink ,$thisPage-$midOffset) ;
  130. $max = min($pageCount,$thisPage $midOffset 1);
  131. $total = $max-$min;
  132. // 這保留了x 數量即使
  133. //您不在第1 頁或第2 頁的中間也會顯示頁數
  134. if($total
  135. if($min==0){
  136. $max =$ displayed-$total;
  137. }
  138. if($max==$pageCount){
  139. $min-=$displayed-$total;
  140. }
  141. }
  142. } else{
  143. # 只輸出一組數字
  144. $min = 0;
  145. $max = $pageCount;
  146. }
  147. //運行頁面,檢查目前頁面並將其命名為
  148. for($i=$min;$i$cp = '否';
  149. if($thisPage==$i)
  150. $cp = '是';
  151. if($max!=$pageCount )
  152. $returnAry['afterdot'] = '...';
  153. if($min>0)
  154. $returnAry['beforedot'] = '...';
  155. array_push($returnAry,
  156. array(
  157. 'currentPage'=>$cp,
  158. 'pageNumber'= >($i 1),
  159. 'pageLink'=>; $this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i 1) )
  160. )
  161. );
  162. }
  163. return $ returnAry;
  164. }
  165. function makePageNumbers($format, $pages,$boldCurrent=true,$,$se)
  166. $retPages = '';
  167. // 產生實際頁碼
  168. foreach($pages as $key => $value):
  169. if(is_numeric($key))
  170. $retPages .= ('yes'==$value ['currentPage'] && $boldCurrent)?''.$value['pageLink'] .''.$separator:$value[' pageLink'].$separator;
  171. endforeach;
  172. $format = str_replace( array('{beforedot}',
  173. '{afterdot}',
  174. '{pages}',
  175. '{afterdot}',
  176. '{pages}',
  177. '{separator}'),
  178. array( $pages['beforedot'],
  179. $pages['afterdot'],
  180. $retPages),
  181. $format);
  182. return $format;
  183. }
  184. ## 檢查
  185. 函數isFirstPage(){
  186. if($this->display_currentpage==0)
  187. 回傳true;
  188. 回傳false ;
  189. }
  190. 函數isLastPage(){
  191. // 加一,因為基數是0,而不是1
  192. if($this->display_currentpage 1==$this->getPageCount())
  193. return true;
  194. return false;
  195. }
  196. ## 函數
  197. function getPageName(){
  198. $fileName =explode('/',$_SERPVER['REQUEST_RIURIU ']);
  199. $fileName = $fileName[count($fileName)-1];
  200. if(strpos($fileName,'?')>0) {
  201. $fileName =explode ('?',$fileName) ;
  202. $fileName = $fileName[0];
  203. }
  204. return $fileName;
  205. }
  206. function getCleanURI(){
  207. $URI = $_SERVER ['REQUEST_URI'];
  208. if(strpos($URI,'?')>0){
  209. $URI =explode('?',$URI);
  210. $URI = $URI[ 1];
  211. $URI = preg_replace('/b'.$this->display_offset_var.'b[=0-9&]{2,20}/','' ,$URI);
  212. //$URI = preg_replace('/b'.$this->display_limit_var.'b[=0-9&]{2,20}/','',$URI);
  213. return $URI;
  214. }
  215. 回傳false;
  216. }
  217. function buildNewURI($offset){
  218. $newFile = $this->getPageName() this。 '? ' 。 $this->getCleanURI();
  219. $lastChar = substr($newFile,strlen($newFile)-1,1);
  220. if( $lastChar != '&' && $lastChar ! = '?' )
  221. $newFile.='&';
  222. $newFile .= $this->display_offset_var.'='.$offset.'&';
  223. //$ newFile . = $this->display_limit_var.'='.$limit.'&';
  224. return $newFile;
  225. }
  226. function buildLink( $href, $caption, $target= NULL ){
  227. if( $target != NULL )
  228. $target = ' target="'.$target.'"';
  229. return ''.$caption.'';
  230. }
  231. functionencodeURI($str){
  232. $salt = 'falaful ';
  233. $str = strrev($salt.$str);
  234. 回傳base64_encode($str);
  235. }
  236. 函數decodeURI($str){
  237. $salt = 'falaful';
  238. $str = strrev( base64_decode($str) );
  239. $str = substr( $str, strlen($salt), strlen($str) );return $str; }
  240. ##############
  241. #
  242. #這些函數用來輸入要執行的
  243. #類別的查詢。其他函數將取得所有
  244. #x 行,然後將其截斷。這些函數將
  245. #only 限制為任意數量。這提高了性能。
  246. #原因是當您需要 3 行時,您不會抓取 1,000,000 行。
  247. #
  248. ##############
  249. function runWQuery ($db, $statement, $table, $where=''){
  250. //取得總行數
  251. $db->query( 'SELECT COUNT(*) AS count FROM '. $表. $where );
  252. $db->movenext();
  253. $this ->alt_content_count = $db->col['count'];
  254. / / 新增限制查詢
  255. $where .= ' LIMIT ' 。 $this->display_offset .', '.$this->display_limit;
  256. // 儲存查詢
  257. $this->sql_query = $statement . ' 從 ' 。 $表。 ' '。 $where;
  258. // 列印查詢
  259. //echo $this->sql_query;
  260. // 執行查詢
  261. $db->query( $this-> ;sql_query );
  262. $this->sql_results = array();
  263. //儲存結果
  264. while($db->movenext()){
  265. array_push($this -> $db->col);
  266. }
  267. return $this->runQueryActions();
  268. }
  269. function runQueryActions(){
  270. $this->setContent( $this->sql_results );
  271. $pages = $this->getPageNumbers();
  272. // 產生實際頁碼
  273. $ retPages = $this->makePageNumbers( '{beforedots( '{beforedot } {pages} {afterdot}', $pages, true, ' ' );
  274. //取得新的顯示
  275. return array(
  276. 'content'=>$this->sql_results,
  277. 'pages'=>$retPages
  278. );
  279. }
  280. }
  281. ?>
複製代碼


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!