ホームページ  >  記事  >  バックエンド開発  >  古典的なPHPページングコード

古典的なPHPページングコード

WBOY
WBOYオリジナル
2016-07-25 08:52:35999ブラウズ
  1. /***************************************
  2. ファイル: class.paging.php
  3. php ページング クラス コード
  4. *************************************/
  5. クラスページング{
  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 _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. 関数ページング($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. ## データ処理
  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 ++$i
  51. )
  52. array_push($returnAry,$this->content[$i]);
  53. return $returnAry;
  54. }
  55. ## ナビゲーションへのリンク
  56. function getLinks(){
  57. $this->getNextLink('');
  58. $this->getFirstLink('');
  59. $this->getLastLink ('');
  60. $this->getPrevLink('');
  61. }
  62. function getNext(){
  63. return $this->display_link_next;
  64. }
  65. function getPrev(){
  66. return $this-> ;display_link_prev;
  67. }
  68. function getFirst(){
  69. return $this->display_link_first;
  70. }
  71. function getLast(){
  72. return $this->display_link_last;
  73. }
  74. function getNextLink($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. return $this->buildLink( $this->buildNewURI( $this-> display_link_next), $caption );
  89. }
  90. function getPrevLink($caption){
  91. $this->gt;display_link_prev = max(
  92. 0,
  93. $this->display_offset-$this->gt;display_limit
  94. );
  95. return $this->buildLink( $this->buildNewURI( $this->display_link_prev), $caption );
  96. }
  97. function getFirstLink($caption){
  98. $this->display_link_first = 0;
  99. return $ this->buildLink( $this->buildNewURI( $this->display_link_first), $caption );
  100. }
  101. function getLastLink($caption){
  102. $this->display_link_last = $this->content_count -$this->display_limit;
  103. return $this->buildLink( $this->buildNewURI( $this->display_link_last), $caption );
  104. }
  105. ## NUMBERS
  106. function getPageCount(){
  107. if( $this->alt_content_count )
  108. $count = $this->alt_content_count;
  109. 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->gt;display_mid_offset;
  119. $firstLink = $this->gt;display_link_first;
  120. $pageCount = $this->getPageCount();
  121. $ thisPage = $this->display_currentpage;
  122. $limit = $this->display_limit;
  123. $displayed=$midOffset*2+1;
  124. $returnAry=array();
  125. // dots
  126. $returnAry['afterdot '] = '';
  127. $returnAry['beforedot'] = '';
  128. // 2 回中央にすると、その数はすべてのページを合わせた値よりも小さくなります
  129. if( ($midOffset*2+1) < $pageCount ){
  130. $min = max($firstLink,$thisPage-$midOffset);
  131. $max = min($pageCount,$thisPage+$midOffset+1);
  132. $total = $max-$min;
  133. / / これにより、
  134. // ページ 1 または 2 の途中ではない場合でも、x 個のページが表示されます
  135. if($total<$displayed){
  136. if($min==0){
  137. $max+= $displayed-$total;
  138. }
  139. if($max==$pageCount){
  140. $min-=$displayed-$total;
  141. }
  142. }
  143. }else{
  144. # 数値のセットを出力するだけです
  145. $min = 0;
  146. $max = $pageCount;
  147. }
  148. // ページを実行し、現在のページを確認し、名前を付けます
  149. for($i=$min;$i<$max;++$i){
  150. $cp = 'いいえ';
  151. if($thisPage==$i)
  152. $cp = 'はい';
  153. if($max!=$pageCount)
  154. $returnAry['afterdot'] = '...';
  155. if($min>0)
  156. $returnAry['beforedot'] = '...';
  157. array_push($returnAry,
  158. array(
  159. 'currentPage'=>$cp,
  160. 'pageNumber'=>) ($i+1),
  161. 'pageLink'=>$this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i+1) )
  162. )
  163. );
  164. }
  165. return $returnAry;
  166. }
  167. function makePageNumbers($format, $pages,$boldCurrent=true,$separator=' '){
  168. $retPages = '';
  169. // 実際のページ番号を作成します
  170. foreach ($pages as $key => $value):
  171. if(is_numeric($key))
  172. $retPages .= ('yes'==$value['currentPage'] && $boldCurrent)?''.$value['pageLink'] .''.$separator:$value['pageLink'].$separator;
  173. endforeach;
  174. $format = str_replace( array('{beforedot}',
  175. '{afterdot}',
  176. ' {pages}',
  177. '{separator}'),
  178. array( $pages['beforedot'],
  179. $pages['afterdot'],
  180. $retPages),
  181. $format);
  182. return $format;
  183. }
  184. ## チェック
  185. function isFirstPage(){
  186. if($this->display_currentpage==0)
  187. return true;
  188. return false;
  189. }
  190. function isLastPage(){
  191. // 基底が 0 なので 1 を追加します、 1 ではありません
  192. if($this->display_currentpage+1==$this->getPageCount())
  193. return true;
  194. return false;
  195. }
  196. ## 関数
  197. function getPageName(){
  198. $fileName =explode ('/',$_SERVER['REQUEST_URI']);
  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. return false;
  216. }
  217. function buildNewURI($offset){
  218. $newFile = $this->getPageName() 。 「?」 。 $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. 関数encodeURI($str){
  232. $salt = 'falaful';
  233. $str = strrev($salt.$str);
  234. returnbase64_encode($str);
  235. }
  236. function decodeURI($str){
  237. $salt = 'falaful';
  238. $str = strrev(base64_decode($str) );
  239. $str = substr( $str, strlen($salt), strlen($str) );
  240. return $str;
  241. }
  242. ##############
  243. #
  244. #これらの関数は、この
  245. #クラスを実行するためのクエリを入力するためのものです。他の関数はすべて
  246. #x 個の行を取得し、それを切り捨てます。これらの関数は
  247. #のみ、任意の量に制限されます。これにより、パフォーマンスが向上します。
  248. #理由は、3 が必要なときに 1,000,000 行を取得しないようにするためです。
  249. #
  250. ###############
  251. function runWQuery($db, $statement, $table, $where=''){
  252. // 合計行数を取得
  253. $db->query( 'SELECT COUNT(*) AS count FROM '. $table . ' ' . $where );
  254. $db->movenext ();
  255. $this->alt_content_count = $db->col['count'];
  256. // クエリに制限を追加します
  257. $where .= ' LIMIT ' 。 $this->display_offset .', '.$this->display_limit;
  258. // クエリを保存
  259. $this->sql_query = $statement . ' から ' 。 $テーブル。' '。 $where;
  260. // print query
  261. //echo $this->gt;sql_query;
  262. // run query
  263. $db->query( $this->>sql_query );
  264. $this->sql_results = array();
  265. // 結果を保存
  266. while($db->movenext()){
  267. array_push($this->sql_results , $db->col);
  268. }
  269. return $this-> ;runQueryActions();
  270. }
  271. function runQueryActions(){
  272. $this->setContent( $this->sql_results );
  273. $pages = $this->getPageNumbers();
  274. // 実際のものにするページ番号
  275. $retPages = $this->makePageNumbers( '{beforedot} {pages} {afterdot}', $pages, true, ' ' );
  276. // 新しい表示を取得
  277. return array(
  278. 'content'= >$this->sql_results,
  279. 'pages'=>$retPages
  280. );
  281. }
  282. }
  283. ?>
复制代码

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。