php分页类 (来源网络)

  1 <?php
  2 namespace publicBundleController;
  3 
  4 use SymfonyBundleFrameworkBundleControllerController;
  5 use SensioBundleFrameworkExtraBundleConfigurationRoute;
  6 
  7 /**
  8  * @author  gf
  9  * 分页封装
 10  * Class PageLinkControoler
 11  * @package publicBundleController;
 12  */
 13 class PageLinkControoler
 14 {
 15     var $page_max = 10; //一组页码的最大数
 16 
 17     var $page_num = 10; //总页数
 18     var $length = 20; //一页的数据条数
 19 
 20     var $isNextPage = true;
 21     var $isFirstPage = false;
 22 
 23     function Calculation_Page_Num( $total )
 24     {
 25         $this->page_num = ceil( $total / $this->length );
 26         return $this->page_num;
 27     }
 28 
 29     function Calculation_Min_Max( $act_page = 1 )
 30     {
 31         // 定义左右偏移量
 32         $py_left = 0;
 33         $py_right = 0;
 34         // 定义左右边界
 35         $bj_left = 0;
 36         $bj_right = 0;
 37         // 定义滚动区间边界
 38         $gd_left = 0;
 39         $gd_right = 0;
 40         // 判断是否需要分组
 41         if ( ( $this->page_num - $this->page_max ) <= 0 )
 42         {
 43             // 不需要分组
 44             $bj_left = 1;
 45             $bj_right = $this->page_num;
 46         }
 47         else
 48         {
 49             // 要进行分组
 50             // 判断容量的奇偶
 51             $tmp = $this->page_max % 2;
 52             if ( $tmp === 1 )
 53             {
 54                 // 奇数
 55                 $py_left = $py_right = ( $this->page_max - 1 ) / 2;
 56             }
 57             else
 58             {
 59                 // 偶数
 60                 $py_left = $this->page_max / 2 - 1;
 61                 $py_right = $this->page_max / 2;
 62             }
 63             // 计算滚动区间
 64             $gd_left = 1 + $py_left;
 65             $gd_right = $this->page_num - $py_right;
 66             // 判断当前页是否落入了滚动区间
 67             if ( $act_page >= $gd_left && $act_page <= $gd_right )
 68             {
 69                 // 区间内
 70                 $bj_left = $act_page - $py_left;
 71                 $bj_right = $act_page + $py_right;
 72             }
 73             else
 74             {
 75                 // 区间外
 76                 if ( ( $act_page - $py_left ) <= 1 )
 77                 {
 78                     // 左侧固定区间
 79                     $bj_left = 1;
 80                     $bj_right = $this->page_max;
 81                 }
 82                 else
 83                 {
 84                     $bj_left = $this->page_num - $this->page_max + 1;
 85                     $bj_right = $this->page_num;
 86                 }
 87             }
 88         }
 89 
 90         $res = array();
 91         $res['min'] = $bj_left;
 92         $res['max'] = $bj_right;
 93 
 94         return $res;
 95 
 96     }
 97     // 主方法
 98     function make_page( $total, $act_page, $url, $param )
 99     {
100         $page_num = $this->Calculation_Page_Num( $total );
101         $arr_min_max = $this->Calculation_Min_Max( $act_page );
102 
103         if (!eregi("([?|&]$param=)", $url)) {
104             $url = strpos($url,"?")===false?$url."?":$url."&";
105             $url = $url."$param=0";
106         }
107 
108         if ( $act_page > $page_num )
109         {
110             $act_page = $page_num;
111         }
112         // 用正则把url改成正规的
113         $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
114 
115         $res = array();
116         $d = 0;
117         for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
118         {
119             if ( $i == $act_page )
120             {
121                 $res[$d]['url'] = '';
122                 $res[$d]['name'] = $i;
123                 $res[$d]['no'] = $i;
124             }
125             else
126             {
127                 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
128                 $res[$d]['name'] = $i;
129                 $res[$d]['no'] = $i;
130             }
131             $d++;
132         }
133 
134         if ( $this->isNextPage )
135         {
136             $res = $this->make_before_next_link( $res, $act_page, $url, $param );
137         }
138         if ( $this->isFirstPage )
139         {
140             $res = $this->make_first_end_link( $res, $act_page, $url, $param );
141         }
142         return $res;
143     }
144     //// 带总页数
145     function make_page_with_total( $total, $act_page, $url, $param )
146     {
147         $page_num = $this->Calculation_Page_Num( $total );
148         $arr_min_max = $this->Calculation_Min_Max( $act_page );
149 
150         if (!eregi("([?|&]$param=)", $url)) {
151             $url = strpos($url,"?")===false?$url."?":$url."&";
152             $url = $url."$param=0";
153         }
154 
155         if ( $act_page > $page_num )
156         {
157             $act_page = $page_num;
158         }
159         // 用正则把url改成正规的
160         $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
161 
162         $res = array();
163         $d = 0;
164         for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
165         {
166             if ( $i == $act_page )
167             {
168                 $res[$d]['url'] = '';
169                 $res[$d]['name'] = $i;
170                 $res[$d]['no'] = $i;
171             }
172             else
173             {
174                 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
175                 $res[$d]['name'] = $i;
176                 $res[$d]['no'] = $i;
177             }
178             $d++;
179         }
180 
181         if ( $this->isNextPage )
182         {
183             $res = $this->make_before_next_link( $res, $act_page, $url, $param );
184         }
185         if ( $this->isFirstPage )
186         {
187             $res = $this->make_first_end_link( $res, $act_page, $url, $param );
188         }
189 
190         $total_num= ceil($total/$this->length);
191         $result['total']=$total_num;
192         $result['DATA']=$res;
193         return $result;
194     }
195 
196     // 附加上一页和下一页
197     function make_before_next_link( $arr, $act, $url, $param )
198     {
199         $tmp = array();
200 
201         $before = $act - 1;
202         $next = $act + 1;
203 
204         if ( $before < 1 )
205         {
206             $before = 1;
207             $tmp[0]['url'] = '';
208             $tmp[0]['name'] = "上一页";
209             $tmp[0]['no'] = $before;
210         }
211         else
212         {
213             $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
214             $tmp[0]['name'] = "上一页";
215             $tmp[0]['no'] = $before;
216         }
217 
218         $counts = sizeof( $arr );
219         $tmp_count = sizeof( $tmp );
220         for( $i = 0;$i < $counts;$i++ )
221         {
222             $tmp[$tmp_count]['url'] = $arr[$i]['url'];
223             $tmp[$tmp_count]['name'] = $arr[$i]['name'];
224             $tmp[$tmp_count]['no'] = $arr[$i]['no'];
225             $tmp_count++;
226         }
227 
228         if ( $next > $this->page_num )
229         {
230             $next = $this->page_num;
231             $tmp[$tmp_count]['url'] = '';
232             $tmp[$tmp_count]['name'] = "下一页";
233             $tmp[$tmp_count]['no'] = $next;
234         }
235         else
236         {
237             $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
238             $tmp[$tmp_count]['name'] = "下一页";
239             $tmp[$tmp_count]['no'] = $next;
240         }
241 
242         return $tmp;
243     }
244 
245     // 附加首页和尾页
246     function make_first_end_link( $arr, $act, $url, $param )
247     {
248         $tmp = array();
249 
250         $before = 1;
251         $next = $this->page_num;
252 
253         if ( $act == 1 )
254         {
255             $before = 1;
256             $tmp[0]['url'] = '';
257             $tmp[0]['name'] = "首页";
258             $tmp[0]['no'] = $before;
259         }
260         else
261         {
262             $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
263             $tmp[0]['name'] = "首页";
264             $tmp[0]['no'] = $before;
265         }
266 
267         $counts = sizeof( $arr );
268         $tmp_count = sizeof( $tmp );
269         for( $i = 0;$i < $counts;$i++ )
270         {
271             $tmp[$tmp_count]['url'] = $arr[$i]['url'];
272             $tmp[$tmp_count]['name'] = $arr[$i]['name'];
273             $tmp[$tmp_count]['no'] = $arr[$i]['no'];
274             $tmp_count++;
275         }
276 
277         if ( $act == $this->page_num )
278         {
279             $tmp[$tmp_count]['url'] = '';
280             $tmp[$tmp_count]['name'] = "尾页";
281             $tmp[$tmp_count]['no'] = $next;
282         }
283         else
284         {
285             $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
286             $tmp[$tmp_count]['name'] = "尾页";
287             $tmp[$tmp_count]['no'] = $next;
288         }
289 
290         return $tmp;
291     }
292 
293 
294     /**
295      * 带上一页<,下一页>,省略号的分页
296      * @param int $total        记录总条数
297      * @param int $act_page        当前页码
298      * @param string $url        url
299      * @param int $maxpageicon    最大显示页码数
300      * @param int $style        上一页,下一页显示样式
301      * @param string $param        url参数
302      */
303     function make_page_with_points( $total,$act_page,$url,$maxpageicon,$style,$param )
304     {
305         $page_num = $this->Calculation_Page_Num( $total );        //总页数
306         $arr_min_max = $this->Calculation_Min_Max( $act_page );        //最大页,最小页
307         if($total==0)
308         {
309             return "";
310 
311         }
312         if( $act_page > $page_num )
313         {
314             $act_page = $page_num+1;
315             $page_num = $page_num+1;
316         }
317 
318         switch ($style){
319             case 1:
320                 $name_before = '前一页';
321                 $name_next = '后一页';
322                 break;
323             case 2:
324                 $name_before = '<';
325                 $name_next = '>';
326                 break;
327             case 3:
328                 $name_before = '<<';
329                 $name_next = '>>';
330                 break;
331             default:
332                 $name_before = '上一页';
333                 $name_next = '下一页';
334         }
335 
336         if (!eregi("([?|&]$param=)", $url)) {
337             $url = strpos($url,"?")===false?$url."?":$url."&";
338             $url = $url."$param=0";
339         }
340 
341         // 用正则把url改成正规的
342         $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
343         $res = array();
344         $no_before = $act_page-1;
345         $no_next = $act_page+1;
346 
347         //总页数如果小于等于初始化最大呈现页数
348         if ($page_num<= ($maxpageicon + 1))
349         {
350             //如果当前页数是首页  上一页无效
351             if ($act_page == 1)
352             {
353                 $res[0]['url'] = '';
354                 $res[0]['name'] = $name_before;
355                 $res[0]['no'] = $no_before;
356             }
357             else            //上一页有效
358             {
359                 $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
360                 $res[0]['name'] = $name_before;
361                 $res[0]['no'] = $no_before;
362             }
363             //循环添加页码
364             $d = 1;
365             for ($i = 1; $i <= $page_num; $i++)
366             {
367                 if ($i != $act_page)
368                 {
369                     $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
370                     $res[$d]['name'] = $i;
371                     $res[$d]['no'] = $i;
372                 }
373                 else    //当前页,页码
374                 {
375                     $res[$d]['url'] = '';
376                     $res[$d]['name'] = $i;
377                     $res[$d]['no'] = $i;
378                     $res[$d]['attr'] = 'current';
379                 }
380                 $d++;
381             }
382             $last_d = count($res);
383             //判断尾页
384             if($act_page == $page_num)    //下一页无效
385             {
386                 $res[$last_d]['url'] = '';
387                 $res[$last_d]['name'] = $name_next;
388                 $res[$last_d]['no'] = $no_next;
389             }
390             else
391             {
392                 $res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url );
393                 $res[$last_d]['name'] = $name_next;
394                 $res[$last_d]['no'] = $no_next;
395             }
396         }else if ($page_num > ($maxpageicon + 1))//如果总页数满足添加省略号
397         {
398             if ($act_page <= $maxpageicon) //如果当前页小于等于初始化数目
399             {
400                 //如果当前页数是首页  上一页无效
401                 if ($act_page == 1)
402                 {
403                     $res[0]['url'] = '';
404                     $res[0]['name'] = $name_before;
405                     $res[0]['no'] = $no_before;
406                 }
407                 else            //上一页有效
408                 {
409                     $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
410                     $res[0]['name'] = $name_before;
411                     $res[0]['no'] = $no_before;
412                 }
413                 //循环添加页码
414                 $d = 1;
415                 for ($i = 1; $i <= $maxpageicon; $i++)
416                 {
417                     if ($i != $act_page)
418                     {
419                         $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
420                         $res[$d]['name'] = $i;
421                         $res[$d]['no'] = $i;
422                     }
423                     else    //当前页,页码
424                     {
425                         $res[$d]['url'] = '';
426                         $res[$d]['name'] = $i;
427                         $res[$d]['no'] = $i;
428                         $res[$d]['attr'] = 'current';
429                     }
430                     $d++;
431                 }
432                 $last_d = count($res);
433                 //添加省略号
434                 $res[$last_d]['url'] = '';
435                 $res[$last_d]['name'] = '...';
436                 $res[$last_d]['no'] = '';
437                 //总页数
438                 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url );
439                 $res[$last_d+1]['name'] = $page_num;
440                 $res[$last_d+1]['no'] = $page_num;
441                 //下一页
442                 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url );
443                 $res[$last_d+1]['name'] = $name_next;
444                 $res[$last_d+1]['no'] = $no_next;
445             }else//如果当前页大于最大显示页面
446             {
447                 if ($act_page > ($page_num - $maxpageicon))//满足后几页
448                 {
449                     //上一页
450                     $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
451                     $res[0]['name'] = $name_before;
452                     $res[0]['no'] = $no_before;
453                     //第一页
454                     $res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url );
455                     $res[1]['name'] = 1;
456                     $res[1]['no'] = 1;
457                     //省略号
458                     $res[2]['url'] = '';
459                     $res[2]['name'] = '...';
460                     $res[2]['no'] = '';
461                     $d = 3;
462                     for ($i = ($page_num - $maxpageicon + 1); $i <= $page_num; $i++)
463                     {
464                         if ($i != $act_page)
465                         {
466                             $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
467                             $res[$d]['name'] = $i;
468                             $res[$d]['no'] = $i;
469                         }
470                         else    //当前页,页码
471                         {
472                             $res[$d]['url'] = '';
473                             $res[$d]['name'] = $i;
474                             $res[$d]['no'] = $i;
475                             $res[$d]['attr'] = 'current';
476                         }
477                         $d++;
478                     }
479                     $last_d = count($res);
480                     //判断尾页
481                     if($act_page == $page_num)    //下一页无效
482                     {
483                         $res[$last_d]['url'] = '';
484                         $res[$last_d]['name'] = $name_next;
485                         $res[$last_d]['no'] = $no_next;
486                     }
487                     else
488                     {
489                         $res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url );
490                         $res[$last_d]['name'] = $name_next;
491                         $res[$last_d]['no'] = $no_next;
492                     }
493 
494                 }else//满足处在中间
495                 {
496                     //上一页
497                     $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
498                     $res[0]['name'] = $name_before;
499                     $res[0]['no'] = $no_before;
500                     //第一页
501                     $res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url );
502                     $res[1]['name'] = 1;
503                     $res[1]['no'] = 1;
504                     //省略号
505                     $res[2]['url'] = '';
506                     $res[2]['name'] = '...';
507                     $res[2]['no'] = '';
508                     for ($i = ($act_page - ($maxpageicon - 2) / 2); $i <= floor($act_page+($maxpageicon - 2) / 2); $i++)
509                     {
510                         $i = ceil($i);
511                         if ($i != $act_page)
512                         {
513                             $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
514                             $res[$d]['name'] = $i;
515                             $res[$d]['no'] = $i;
516                         }
517                         else    //当前页,页码
518                         {
519                             $res[$d]['url'] = '';
520                             $res[$d]['name'] = $i;
521                             $res[$d]['no'] = $i;
522                             $res[$d]['attr'] = 'current';
523                         }
524                         $d++;
525                     }
526                     $last_d = count($res);
527                     //加省略号
528                     $res[$last_d]['url'] = '';
529                     $res[$last_d]['name'] = '...';
530                     $res[$last_d]['no'] = '';
531                     //当前页
532                     $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url );
533                     $res[$last_d+1]['name'] = $page_num;
534                     $res[$last_d+1]['no'] = $page_num;
535                     //下一页
536                     $res[$last_d+2]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url );
537                     $res[$last_d+2]['name'] = $name_next;
538                     $res[$last_d+2]['no'] = $no_next;
539                     //exit;
540                 }
541             }
542         }
543         return $res;
544     }
545 }
546 
547 ?>

操作方法

$page_no = $_GET['page_no'] ? $_GET['page_no'] : 1;
$count = 20;
$start = ($page_no-1)*$count;
$end = $start + $count;
$page = new Page_Link();
$page->length = $count;
$page->isFirstPage = true;
$url = $_SERVER['SCRIPT_NAME']."?&a=".$a;
$page_arr = $page->make_page($total_num,$page_no,$url,"page_no");
<?php
namespace publicBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;

/**
* @author gf
* 分页封装
* Class PageLinkControoler
* @package publicBundleController;
*/
class PageLinkControoler
{
var $page_max = 10; //一组页码的最大数

var $page_num = 10; //总页数
var $length = 20; //一页的数据条数

var $isNextPage = true;
var $isFirstPage = false;

function Calculation_Page_Num( $total )
{
$this->page_num = ceil( $total / $this->length );
return $this->page_num;
}

function Calculation_Min_Max( $act_page = 1 )
{
// 定义左右偏移量
$py_left = 0;
$py_right = 0;
// 定义左右边界
$bj_left = 0;
$bj_right = 0;
// 定义滚动区间边界
$gd_left = 0;
$gd_right = 0;
// 判断是否需要分组
if ( ( $this->page_num - $this->page_max ) <= 0 )
{
// 不需要分组
$bj_left = 1;
$bj_right = $this->page_num;
}
else
{
// 要进行分组
// 判断容量的奇偶
$tmp = $this->page_max % 2;
if ( $tmp === 1 )
{
// 奇数
$py_left = $py_right = ( $this->page_max - 1 ) / 2;
}
else
{
// 偶数
$py_left = $this->page_max / 2 - 1;
$py_right = $this->page_max / 2;
}
// 计算滚动区间
$gd_left = 1 + $py_left;
$gd_right = $this->page_num - $py_right;
// 判断当前页是否落入了滚动区间
if ( $act_page >= $gd_left && $act_page <= $gd_right )
{
// 区间内
$bj_left = $act_page - $py_left;
$bj_right = $act_page + $py_right;
}
else
{
// 区间外
if ( ( $act_page - $py_left ) <= 1 )
{
// 左侧固定区间
$bj_left = 1;
$bj_right = $this->page_max;
}
else
{
$bj_left = $this->page_num - $this->page_max + 1;
$bj_right = $this->page_num;
}
}
}

$res = array();
$res['min'] = $bj_left;
$res['max'] = $bj_right;

return $res;

}
// 主方法
function make_page( $total, $act_page, $url, $param )
{
$page_num = $this->Calculation_Page_Num( $total );
$arr_min_max = $this->Calculation_Min_Max( $act_page );

if (!eregi("([?|&]$param=)", $url)) {
$url = strpos($url,"?")===false?$url."?":$url."&";
$url = $url."$param=0";
}

if ( $act_page > $page_num )
{
$act_page = $page_num;
}
// 用正则把url改成正规的
$url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );

$res = array();
$d = 0;
for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
{
if ( $i == $act_page )
{
$res[$d]['url'] = '';
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
else
{
$res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
$d++;
}

if ( $this->isNextPage )
{
$res = $this->make_before_next_link( $res, $act_page, $url, $param );
}
if ( $this->isFirstPage )
{
$res = $this->make_first_end_link( $res, $act_page, $url, $param );
}
return $res;
}
//// 带总页数
function make_page_with_total( $total, $act_page, $url, $param )
{
$page_num = $this->Calculation_Page_Num( $total );
$arr_min_max = $this->Calculation_Min_Max( $act_page );

if (!eregi("([?|&]$param=)", $url)) {
$url = strpos($url,"?")===false?$url."?":$url."&";
$url = $url."$param=0";
}

if ( $act_page > $page_num )
{
$act_page = $page_num;
}
// 用正则把url改成正规的
$url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );

$res = array();
$d = 0;
for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
{
if ( $i == $act_page )
{
$res[$d]['url'] = '';
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
else
{
$res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
$d++;
}

if ( $this->isNextPage )
{
$res = $this->make_before_next_link( $res, $act_page, $url, $param );
}
if ( $this->isFirstPage )
{
$res = $this->make_first_end_link( $res, $act_page, $url, $param );
}

$total_num= ceil($total/$this->length);
$result['total']=$total_num;
$result['DATA']=$res;
return $result;
}

// 附加上一页和下一页
function make_before_next_link( $arr, $act, $url, $param )
{
$tmp = array();

$before = $act - 1;
$next = $act + 1;

if ( $before < 1 )
{
$before = 1;
$tmp[0]['url'] = '';
$tmp[0]['name'] = "上一页";
$tmp[0]['no'] = $before;
}
else
{
$tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
$tmp[0]['name'] = "上一页";
$tmp[0]['no'] = $before;
}

$counts = sizeof( $arr );
$tmp_count = sizeof( $tmp );
for( $i = 0;$i < $counts;$i++ )
{
$tmp[$tmp_count]['url'] = $arr[$i]['url'];
$tmp[$tmp_count]['name'] = $arr[$i]['name'];
$tmp[$tmp_count]['no'] = $arr[$i]['no'];
$tmp_count++;
}

if ( $next > $this->page_num )
{
$next = $this->page_num;
$tmp[$tmp_count]['url'] = '';
$tmp[$tmp_count]['name'] = "下一页";
$tmp[$tmp_count]['no'] = $next;
}
else
{
$tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
$tmp[$tmp_count]['name'] = "下一页";
$tmp[$tmp_count]['no'] = $next;
}

return $tmp;
}

// 附加首页和尾页
function make_first_end_link( $arr, $act, $url, $param )
{
$tmp = array();

$before = 1;
$next = $this->page_num;

if ( $act == 1 )
{
$before = 1;
$tmp[0]['url'] = '';
$tmp[0]['name'] = "首页";
$tmp[0]['no'] = $before;
}
else
{
$tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
$tmp[0]['name'] = "首页";
$tmp[0]['no'] = $before;
}

$counts = sizeof( $arr );
$tmp_count = sizeof( $tmp );
for( $i = 0;$i < $counts;$i++ )
{
$tmp[$tmp_count]['url'] = $arr[$i]['url'];
$tmp[$tmp_count]['name'] = $arr[$i]['name'];
$tmp[$tmp_count]['no'] = $arr[$i]['no'];
$tmp_count++;
}

if ( $act == $this->page_num )
{
$tmp[$tmp_count]['url'] = '';
$tmp[$tmp_count]['name'] = "尾页";
$tmp[$tmp_count]['no'] = $next;
}
else
{
$tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
$tmp[$tmp_count]['name'] = "尾页";
$tmp[$tmp_count]['no'] = $next;
}

return $tmp;
}


/**
* 带上一页<,下一页>,省略号的分页
* @param int $total 记录总条数
* @param int $act_page 当前页码
* @param string $url url
* @param int $maxpageicon 最大显示页码数
* @param int $style 上一页,下一页显示样式
* @param string $param url参数
*/
function make_page_with_points( $total,$act_page,$url,$maxpageicon,$style,$param )
{
$page_num = $this->Calculation_Page_Num( $total ); //总页数
$arr_min_max = $this->Calculation_Min_Max( $act_page ); //最大页,最小页
if($total==0)
{
return "";

}
if( $act_page > $page_num )
{
$act_page = $page_num+1;
$page_num = $page_num+1;
}

switch ($style){
case 1:
$name_before = '前一页';
$name_next = '后一页';
break;
case 2:
$name_before = '<';
$name_next = '>';
break;
case 3:
$name_before = '<<';
$name_next = '>>';
break;
default:
$name_before = '上一页';
$name_next = '下一页';
}

if (!eregi("([?|&]$param=)", $url)) {
$url = strpos($url,"?")===false?$url."?":$url."&";
$url = $url."$param=0";
}

// 用正则把url改成正规的
$url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
$res = array();
$no_before = $act_page-1;
$no_next = $act_page+1;

//总页数如果小于等于初始化最大呈现页数
if ($page_num<= ($maxpageicon + 1))
{
//如果当前页数是首页 上一页无效
if ($act_page == 1)
{
$res[0]['url'] = '';
$res[0]['name'] = $name_before;
$res[0]['no'] = $no_before;
}
else //上一页有效
{
$res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
$res[0]['name'] = $name_before;
$res[0]['no'] = $no_before;
}
//循环添加页码
$d = 1;
for ($i = 1; $i <= $page_num; $i++)
{
if ($i != $act_page)
{
$res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
else //当前页,页码
{
$res[$d]['url'] = '';
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
$res[$d]['attr'] = 'current';
}
$d++;
}
$last_d = count($res);
//判断尾页
if($act_page == $page_num) //下一页无效
{
$res[$last_d]['url'] = '';
$res[$last_d]['name'] = $name_next;
$res[$last_d]['no'] = $no_next;
}
else
{
$res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url );
$res[$last_d]['name'] = $name_next;
$res[$last_d]['no'] = $no_next;
}
}else if ($page_num > ($maxpageicon + 1))//如果总页数满足添加省略号
{
if ($act_page <= $maxpageicon) //如果当前页小于等于初始化数目
{
//如果当前页数是首页 上一页无效
if ($act_page == 1)
{
$res[0]['url'] = '';
$res[0]['name'] = $name_before;
$res[0]['no'] = $no_before;
}
else //上一页有效
{
$res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
$res[0]['name'] = $name_before;
$res[0]['no'] = $no_before;
}
//循环添加页码
$d = 1;
for ($i = 1; $i <= $maxpageicon; $i++)
{
if ($i != $act_page)
{
$res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
else //当前页,页码
{
$res[$d]['url'] = '';
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
$res[$d]['attr'] = 'current';
}
$d++;
}
$last_d = count($res);
//添加省略号
$res[$last_d]['url'] = '';
$res[$last_d]['name'] = '...';
$res[$last_d]['no'] = '';
//总页数
$res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url );
$res[$last_d+1]['name'] = $page_num;
$res[$last_d+1]['no'] = $page_num;
//下一页
$res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url );
$res[$last_d+1]['name'] = $name_next;
$res[$last_d+1]['no'] = $no_next;
}else//如果当前页大于最大显示页面
{
if ($act_page > ($page_num - $maxpageicon))//满足后几页
{
//上一页
$res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
$res[0]['name'] = $name_before;
$res[0]['no'] = $no_before;
//第一页
$res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url );
$res[1]['name'] = 1;
$res[1]['no'] = 1;
//省略号
$res[2]['url'] = '';
$res[2]['name'] = '...';
$res[2]['no'] = '';
$d = 3;
for ($i = ($page_num - $maxpageicon + 1); $i <= $page_num; $i++)
{
if ($i != $act_page)
{
$res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
else //当前页,页码
{
$res[$d]['url'] = '';
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
$res[$d]['attr'] = 'current';
}
$d++;
}
$last_d = count($res);
//判断尾页
if($act_page == $page_num) //下一页无效
{
$res[$last_d]['url'] = '';
$res[$last_d]['name'] = $name_next;
$res[$last_d]['no'] = $no_next;
}
else
{
$res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url );
$res[$last_d]['name'] = $name_next;
$res[$last_d]['no'] = $no_next;
}

}else//满足处在中间
{
//上一页
$res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url );
$res[0]['name'] = $name_before;
$res[0]['no'] = $no_before;
//第一页
$res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url );
$res[1]['name'] = 1;
$res[1]['no'] = 1;
//省略号
$res[2]['url'] = '';
$res[2]['name'] = '...';
$res[2]['no'] = '';
for ($i = ($act_page - ($maxpageicon - 2) / 2); $i <= floor($act_page+($maxpageicon - 2) / 2); $i++)
{
$i = ceil($i);
if ($i != $act_page)
{
$res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
}
else //当前页,页码
{
$res[$d]['url'] = '';
$res[$d]['name'] = $i;
$res[$d]['no'] = $i;
$res[$d]['attr'] = 'current';
}
$d++;
}
$last_d = count($res);
//加省略号
$res[$last_d]['url'] = '';
$res[$last_d]['name'] = '...';
$res[$last_d]['no'] = '';
//当前页
$res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url );
$res[$last_d+1]['name'] = $page_num;
$res[$last_d+1]['no'] = $page_num;
//下一页
$res[$last_d+2]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url );
$res[$last_d+2]['name'] = $name_next;
$res[$last_d+2]['no'] = $no_next;
//exit;
}
}
}
return $res;
}
}

?>
原文地址:https://www.cnblogs.com/g825482785/p/page.html