php 一个超强分页类的完整代码

内容摘要
这篇文章主要为大家详细介绍了php 一个超强分页类的完整代码,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:

<?php
/**
文章正文

这篇文章主要为大家详细介绍了php 一个超强分页类的完整代码,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<code class="php">
<?php
/**
 * 超强分页类
 *
 * @param
 * @arrange (www.idcnote.com)
 **/
class paging{
  
 var $sql_results;
 var $sql_query;
  
 var $display_offset;
 var $display_limit;
 var $display_limit_URI;
 var $display_currentpage;
 var $display_totalpages;
 var $alt_content_count;
  
 var $display_offset_var;
 var $display_limit_var;
  
 var $display_mid_offset;
 var $display_link_first;
 var $display_link_prev;
 var $display_link_next;
 var $display_link_last;
  
 var $page_filename;
 var $page_uri;
  
 var $content;
 var $content_type;
 var $content_count;
  
 ## CONSTRUCTOR
 function paging($offsetVar='offset',$limit=10,$midOffset=2){
  $this->display_offset   = isset($_REQUEST[$offsetVar])?$_REQUEST[$offsetVar]:0;
  $this->display_limit   = is_numeric($limit)?$limit:10;
  $this->display_mid_offset  = $midOffset;
  $this->display_offset_var  = $offsetVar;
  $this->display_currentpage = ceil($this->display_offset / $this->display_limit);
 }
  
 ## HANDLE DATA
 function setContent($content){
  if( is_array($content) )
   $this->content_type = 'array';
  
  $this->content = $content;
  $this->private_contentDetails();
  $this->getLinks();
 }
  
 function private_contentDetails(){
  if( $this->content_type == 'array' )
   $this->content_count = count($this->content);
 }
  
 function getContent(){
  $returnAry = array();
  for(
   $i=$this->display_offset;
   $i< min( $this->content_count, $this->display_offset+$this->display_limit );
    ++$i
   )
   array_push($returnAry,$this->content[$i]);
  
  return $returnAry;
 }
  
 ## LINKS TO NAVIGATE
 function getLinks(){
  $this->getNextLink('');
  $this->getFirstLink('');
  $this->getLastLink('');
  $this->getPrevLink(''); 
 }
  
 function getNext(){
  return $this->display_link_next;
 }
  
 function getPrev(){
  return $this->display_link_prev;
 }
  
 function getFirst(){
  return $this->display_link_first;
 }
  
 function getLast(){
  return $this->display_link_last;
 }
  
 function getNextLink($caption){
  // Check if we're counting content or the alternate user provided count
  if( $this->alt_content_count )
   $count = $this->alt_content_count;
  else
   $count = $this->content_count;
  
  if( $this->display_offset+$this->display_limit-$count >=0 ){
  
   $this->display_link_next = $this->display_offset;
  }else{
   $this->display_link_next = min(
     $count-1,
     $this->display_offset+$this->display_limit
     );
  }
  return $this->buildLink( $this->buildNewURI( $this->display_link_next), $caption );
 }
  
 function getPrevLink($caption){
  $this->display_link_prev = max(
    0,
    $this->display_offset-$this->display_limit
    );
  return $this->buildLink( $this->buildNewURI( $this->display_link_prev), $caption );
 }
  
 function getFirstLink($caption){
  $this->display_link_first = 0;
  return $this->buildLink( $this->buildNewURI( $this->display_link_first), $caption );
 }
  
 function getLastLink($caption){
  $this->display_link_last = $this->content_count-$this->display_limit;
  return $this->buildLink( $this->buildNewURI( $this->display_link_last), $caption );
 }
  
 ## NUMBERS
 function getPageCount(){
  if( $this->alt_content_count )
   $count = $this->alt_content_count;
  else
   $count = $this->content_count;
  
  $this->display_totalpages = ceil(
        $count / $this->display_limit
        );
  return $this->display_totalpages;
 }
  
 function getPageNumbers(){
  
  // define variables
  $midOffset  = $this->display_mid_offset;
  $firstLink  = $this->display_link_first;
  $pageCount  = $this->getPageCount();
  $thisPage  = $this->display_currentpage;
  $limit   = $this->display_limit;
  $displayed = $midOffset*2+1;
  $returnAry = array();
  
  // dots
  $returnAry['afterdot'] = '';
  $returnAry['beforedot'] = '';
  
  
  // if two times and center the number is less than all the pages together
  if( ($midOffset*2+1) < $pageCount ){
   $min = max($firstLink,$thisPage-$midOffset);
   $max = min($pageCount,$thisPage+$midOffset+1);
   $total = $max-$min;
  
   // this keeps x amount of pages displayed even if
   // you're not in mid cause of page 1 or 2
   if($total<$displayed){
  
    if($min==0){
     $max+=$displayed-$total;
    }
    if($max==$pageCount){
     $min-=$displayed-$total;
    }
   }
  
  }else{
   # Just output a set of numbers
   $min = 0;
   $max = $pageCount;
  }
  
  // run pages, check for current page and name it
  for($i=$min;$i<$max;++$i){
   $cp = 'no';
   if($thisPage==$i)
    $cp = 'yes';
  
   if($max!=$pageCount)
    $returnAry['afterdot'] = '...';
  
   if($min>0)
    $returnAry['beforedot'] = '...';
  
   array_push($returnAry,
      array(
       'currentPage' => $cp,
       'pageNumber' => ($i+1),
       'pageLink'  => $this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i+1) )
       )
      );    
  }
  
  return $returnAry;
  
 }
  
 function makePageNumbers($format, $pages,$boldCurrent=true,$separator=' '){
  $retPages = '';
  
 // make actual page numbers
  foreach($pages as $key => $value):
   if(is_numeric($key))
    $retPages .= ('yes'==$value['currentPage'] && $boldCurrent)?'<b>'.$value['pageLink'] .'</b>'.$separator:$value['pageLink'].$separator;
  endforeach;
  
  $format = str_replace( array('{beforedot}',
         '{afterdot}',
         '{pages}',
         '{separator}'),
        array( $pages['beforedot'],
          $pages['afterdot'],
          $retPages),
        $format);
  return $format;
 }
  
 ## CHECKS
 function isFirstPage(){
  if($this->display_currentpage==0)
   return true;
  return false;
 }
 function isLastPage(){
  // add one because basis is 0, not 1
  if($this->display_currentpage+1==$this->getPageCount())
   return true;
  return false;
 }
  
 ## FUNCTIONS
 function getPageName(){
  $fileName = explode('/',$_SERVER['REQUEST_URI']);
  $fileName = $fileName[count($fileName)-1];
  
  if(strpos($fileName,'?')>0) {
   $fileName = explode('?',$fileName);
   $fileName = $fileName[0];
  }
  
  return $fileName;
 }
  
 function getCleanURI(){
  $URI = $_SERVER['REQUEST_URI'];
  if(strpos($URI,'?')>0){
   $URI = explode('?',$URI);
   $URI = $URI[1];
  
   $URI = preg_replace('/\b'.$this->display_offset_var.'\b[=0-9&]{2,20}/','',$URI);
   //$URI = preg_replace('/\b'.$this->display_limit_var.'\b[=0-9&]{2,20}/','',$URI);
  
   return $URI;
  }
  return false;
 }
  
 function buildNewURI($offset){
  $newFile = $this->getPageName() . '?' . $this->getCleanURI();
  
  $lastChar = substr($newFile,strlen($newFile)-1,1);
  if( $lastChar != '&' && $lastChar != '?' )
   $newFile.='&';
  
  $newFile .= $this->display_offset_var.'='.$offset.'&';
  //$newFile .= $this->display_limit_var.'='.$limit.'&';
  
  return $newFile;
 }
  
 function buildLink( $href, $caption, $target=NULL ){
  if( $target != NULL )
   $target = ' target="'.$target.'"';
  return '<a href="'.$href.'"'.$target.'>'.$caption.'</a>';
 }
  
 function encodeURI($str){
  $salt = 'falaful';
  $str = strrev($salt.$str);
  return base64_encode($str);
 }
  
 function decodeURI($str){
  $salt = 'falaful';
  $str = strrev( base64_decode($str) );
  $str = substr( $str, strlen($salt), strlen($str) );
  return $str;
 }
  
  
  
 ##############
 #
 # These functions are for inputting a query for this
 # class to execute. The other functions will grab all
 # x amount of rows then truncate it. These functions will
 # only limit to whatever amount. This improves performance.
 # Reason is so you dont grab 1,000,000 rows when you want 3.
 #
 ##############
  
 function runWQuery($db, $statement, $table, $where=''){
  
  // get total rows
  $db->query( 'SELECT COUNT(*) AS count FROM '. $table . ' ' . $where );
  $db->movenext();
  $this->alt_content_count = $db->col['count'];
  
  // add limit to query
  $where .= ' LIMIT ' . $this->display_offset .', '.$this->display_limit;
  
  // save query
  $this->sql_query = $statement . ' FROM ' . $table .' '. $where;
  
  // print query
  //echo $this->sql_query;
  
  // run query
  $db->query( $this->sql_query );
  $this->sql_results = array();
  
  // save results
  while($db->movenext()){
   array_push($this->sql_results , $db->col);
  }
  
  return $this->runQueryActions();
 }
  
 function runQueryActions(){
  $this->setContent( $this->sql_results );
  
  $pages = $this->getPageNumbers();
  
  // make actual page numbers
  $retPages = $this->makePageNumbers( '{beforedot} {pages} {afterdot}', $pages, true, ' ' );
  
  // get new display
  return array(
     'content' => $this->sql_results,
     'pages'  => $retPages
     );
 }
}
  
 
 
 
/*** 来自:php教程(www.idcnote.com) ***/
?></code>
专门用户数据库查询后分页显示的php类

注:关于php 一个超强分页类的完整代码的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!