首页 > 代码库 > PHP 自写的分页

PHP 自写的分页

fenye.php

 1 <?php 2  3 /** 4  * @param  待分页数组 必须 5  * @param  第几页 必须 6  * @param  每页数量 可选,不填等于10 7  */ 8 function fenye($arr, $page, $countofpage = 10) { 9     /* 总记录数 */10     $totalcount = count($arr);11     /* 总的分页数 */12     $numofpage = ceil($totalcount / $countofpage);13     /* 起始记录基数,数组的下表从0开始,所以$start的最小值为0,并以$countofpage为倍数递增 */14     $start = (($page - 1) * $countofpage);15     /* 结束记录基数 */16     $end = $page * $countofpage;17     /* 边界处理 */18     if ($page == $numofpage) {19         $end = $totalcount;20     } else if ($page > $numofpage) {21         /* 非法输入时,定位到第一页 */22         $start = 0;23         $end = $countofpage;24     }25     /* 目标数据的下表区间,比如第一页,10条记录,区间为:[0,10) */26     for ($i = $start; $i < $end; $i++) {27         $num[$i] = $i;28     }29     foreach ($arr as $key => $value) {30         if (in_array($key, $num)) {31             $temp[$key] = $value;32         }33     }34     return $temp;35 }36 37 /**38  * 返回分页下标,如前一页,下一页,第一页,最后一页等,显示分页的数量和页数39  * @param type 分页的数组40  * @param type 当前页41  * @param type 在数组里的下标42  * @param type 每页的数量43  * @return 二维数组44  */45 function getlastpage($arr, $page, $index, $countofpage = 10) {46     /* 总记录数 */47     $totalcount = count($arr);48     /* 总的分页数 */49     $numofpage = ceil($totalcount / $countofpage);50     /* 上一页 */51     $temp[$index][‘before‘] = $page - 1;52     /* 当前页 */53     $temp[$index][‘page‘] = $page;54     /* 下一页 */55     $temp[$index][‘next‘] = $page + 1;56     /* 最后一页 */57     $temp[$index][‘last‘] = $numofpage;58     /* 总记录数 */59     $temp[$index][‘total‘] = $totalcount;60     return $temp;61 }62 63 /**64  * 65  * @param  分页的数组66  * @param  在数组里的下标67  * @return 二维数组68  */69 function showheader($arr = ‘‘, $index = ‘‘) {70     $temp = array();71     if (!empty($arr)) {72         $temp[$index] = $arr;73     }74     return $temp;75 }76 77 /**78  * 合并表头和分页内容79  * @param type $arr 分页内容80  * @param type $page 当前页81  * @param type $option 表头内容82  * @param type $countofpage 每页数量83  * @return type 二维数组84  */85 function showtable($arr, $option = ‘‘, $page = 1, $countofpage = 10) {86     $result = array_merge(87             showheader($option, $countofpage + 1), 88             getlastpage($arr, $page, $countofpage + 2, $countofpage), 89             fenye($arr, $page, $countofpage)90     );91     return $result;92 }

通过函数showtable()合并三个数组,分别是表头数组,翻页数组和内容数组。

表头数组为可选参数,表示每一列的值的意思,翻页数组顾名思义是用来存放翻页按钮的数值的,内容数组存放翻页的内容。

该分页方法的好处是只需要调用showtable()方法并传入对应的参数,就可以返回一个包装好的二维数组,通过在模板上遍历和现实,实现分页。

例子:

引用:include ‘./fenye.php‘;

准备表头数组:

1     $option[‘id‘] = "id";2     $option[‘bno‘] = "栋号";3     $option[‘rno‘] = "宿舍号";4     $option[‘rtel‘] = "宿舍电话";5     $option[‘rsir‘] = "宿舍长";6     $option[‘rstate‘] = "是否可入住";7     $option[‘rbed‘] = "剩余床位";8     $option[‘rcop‘] = "管理宿舍";

准备分页内容数组:

1 $sql = "select * from room order by rid";2 $result = $pdo->query($sql);3 $arr = $result->fetchAll(PDO::FETCH_ASSOC);

指定每页数量和第几页:

1 $countofpage = 10;2 $page = 1;

调用函数,打印结果:

1 $resu = showtable($arr, $option, $page, $countofpage);2 var_dump($resu);die;

通过改变页数,实现显示不同的分页内容,以下是打印第一页,每页包含十条数据的二维数组:

  1 array  2   0 =>   3     array  4       ‘id‘ => string ‘id‘ (length=2)  5       ‘bno‘ => string ‘栋号‘ (length=6)  6       ‘rno‘ => string ‘宿舍号‘ (length=9)  7       ‘rtel‘ => string ‘宿舍电话‘ (length=12)  8       ‘rsir‘ => string ‘宿舍长‘ (length=9)  9       ‘rstate‘ => string ‘是否可入住‘ (length=15) 10       ‘rbed‘ => string ‘剩余床位‘ (length=12) 11       ‘rcop‘ => string ‘管理宿舍‘ (length=12) 12   1 =>  13     array 14       ‘before‘ => int 0 15       ‘page‘ => string ‘1‘ (length=1) 16       ‘next‘ => int 2 17       ‘last‘ => float 3 18       ‘total‘ => int 28 19   2 =>  20     array 21       ‘rid‘ => string ‘1‘ (length=1) 22       ‘rno‘ => string ‘414‘ (length=3) 23       ‘rbno‘ => string ‘D栋栋‘ (length=7) 24       ‘tel‘ => string ‘13445678901‘ (length=11) 25       ‘hr‘ => string ‘tom‘ (length=3) 26       ‘state‘ => string ‘可入住‘ (length=9) 27       ‘hb‘ => string ‘1‘ (length=1) 28   3 =>  29     array 30       ‘rid‘ => string ‘3‘ (length=1) 31       ‘rno‘ => string ‘416‘ (length=3) 32       ‘rbno‘ => string ‘D栋‘ (length=4) 33       ‘tel‘ => string ‘13445678901‘ (length=11) 34       ‘hr‘ => string ‘as‘ (length=2) 35       ‘state‘ => string ‘可入住‘ (length=9) 36       ‘hb‘ => string ‘2‘ (length=1) 37   4 =>  38     array 39       ‘rid‘ => string ‘4‘ (length=1) 40       ‘rno‘ => string ‘416‘ (length=3) 41       ‘rbno‘ => string ‘D栋‘ (length=4) 42       ‘tel‘ => string ‘13445678901‘ (length=11) 43       ‘hr‘ => string ‘as‘ (length=2) 44       ‘state‘ => string ‘可入住‘ (length=9) 45       ‘hb‘ => string ‘2‘ (length=1) 46   5 =>  47     array 48       ‘rid‘ => string ‘5‘ (length=1) 49       ‘rno‘ => string ‘416‘ (length=3) 50       ‘rbno‘ => string ‘D栋‘ (length=4) 51       ‘tel‘ => string ‘13445678901‘ (length=11) 52       ‘hr‘ => string ‘as‘ (length=2) 53       ‘state‘ => string ‘可入住‘ (length=9) 54       ‘hb‘ => string ‘2‘ (length=1) 55   6 =>  56     array 57       ‘rid‘ => string ‘6‘ (length=1) 58       ‘rno‘ => string ‘416‘ (length=3) 59       ‘rbno‘ => string ‘D栋‘ (length=4) 60       ‘tel‘ => string ‘13445678901‘ (length=11) 61       ‘hr‘ => string ‘as‘ (length=2) 62       ‘state‘ => string ‘可入住‘ (length=9) 63       ‘hb‘ => string ‘2‘ (length=1) 64   7 =>  65     array 66       ‘rid‘ => string ‘7‘ (length=1) 67       ‘rno‘ => string ‘416‘ (length=3) 68       ‘rbno‘ => string ‘D栋‘ (length=4) 69       ‘tel‘ => string ‘13445678901‘ (length=11) 70       ‘hr‘ => string ‘as‘ (length=2) 71       ‘state‘ => string ‘可入住‘ (length=9) 72       ‘hb‘ => string ‘2‘ (length=1) 73   8 =>  74     array 75       ‘rid‘ => string ‘8‘ (length=1) 76       ‘rno‘ => string ‘416‘ (length=3) 77       ‘rbno‘ => string ‘D栋‘ (length=4) 78       ‘tel‘ => string ‘13445678901‘ (length=11) 79       ‘hr‘ => string ‘as‘ (length=2) 80       ‘state‘ => string ‘可入住‘ (length=9) 81       ‘hb‘ => string ‘2‘ (length=1) 82   9 =>  83     array 84       ‘rid‘ => string ‘9‘ (length=1) 85       ‘rno‘ => string ‘416‘ (length=3) 86       ‘rbno‘ => string ‘D栋‘ (length=4) 87       ‘tel‘ => string ‘13445678901‘ (length=11) 88       ‘hr‘ => string ‘as‘ (length=2) 89       ‘state‘ => string ‘可入住‘ (length=9) 90       ‘hb‘ => string ‘2‘ (length=1) 91   10 =>  92     array 93       ‘rid‘ => string ‘10‘ (length=2) 94       ‘rno‘ => string ‘416‘ (length=3) 95       ‘rbno‘ => string ‘D栋‘ (length=4) 96       ‘tel‘ => string ‘13445678901‘ (length=11) 97       ‘hr‘ => string ‘as‘ (length=2) 98       ‘state‘ => string ‘可入住‘ (length=9) 99       ‘hb‘ => string ‘2‘ (length=1)100   11 => 101     array102       ‘rid‘ => string ‘11‘ (length=2)103       ‘rno‘ => string ‘416‘ (length=3)104       ‘rbno‘ => string ‘D栋‘ (length=4)105       ‘tel‘ => string ‘13445678901‘ (length=11)106       ‘hr‘ => string ‘as‘ (length=2)107       ‘state‘ => string ‘可入住‘ (length=9)108       ‘hb‘ => string ‘2‘ (length=1)

最后把返回的一个二维数组通过模板显示出来:

 1 <table> 2 <tr bgcolor="#FFFFFF"> 3     <td >{$resu[0][‘id‘]}</td> 4     <td >{$resu[0][‘bno‘]}</td> 5     <td >{$resu[0][‘rno‘]}</td> 6     <td >{$resu[0][‘rtel‘]}</td> 7     <td >{$resu[0][‘rsir‘]}</td> 8     <td >{$resu[0][‘rstate‘]}</td> 9     <td >{$resu[0][‘rbed‘]}</td>10     <td >{$resu[0][‘rcop‘]}</td>11 </tr>12 {foreach item=row from=$resu key=mykey}13 {if $mykey>1}14 <tr bgcolor="#FFFFFF">15     <td >{$row["rid"]}</td>16     <td >{$row["rbno"]}</td>17     <td >{$row["rno"]}</td>18     <td >{$row["tel"]}</td>19     <td>{$row["hr"]}</td>20     <td>{$row["state"]}</td>21     <td>{$row["hb"]}</td>22     <td width="12%">23         <a href="room_del.php?rid={$row[‘rid‘]}" onclick="return confirm‘你真的确定要删除吗?‘);">删除</a>24         <a href="room_modify.php?rid={$row[‘rid‘]}">修改</a>25     </td>26 </tr>27 {/if}28 {/foreach}29 </tabale>30 <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">31     <tr>32         {if $resu[1][‘page‘]!=1}33             <td><a href="room_show.php?act=testing&page=1">first</a></td>34             <td><a href="room_show.php?act=testing&page={$resu[1][‘before‘]}">before</a></td>35         {/if}36         {if $resu[1][‘page‘]!= $resu[1][‘last‘]}37             <td><a href="room_show.php?act=testing&page={$resu[1][‘next‘]}">next</a></td>38             <td><a href="room_show.php?act=testing&page={$resu[1][‘last‘]}">last</a></td>39         {/if}40     </tr>41 </table>

通过简单的表格显示出来,如下图:

技术分享

拓展:

1、可以拓展函数showtable(),把分页内容里需要的链接通过数组传进去,同样的在模板上分配显示;

2、可以写一个大众化的模板,并加上css修饰,一起打包成分页工具;

3、每次翻页都会重新去查询数据库,如果数据库的数据记录特别多的话,此方法效率上可能不理想,可以使用javascript接收一个分页的数组或对象,通过下表键值检索数组对象,把目标数据传给模板,显示,避免重复查询数据库。

 

PHP 自写的分页