PHP分页函数
来源:互联网 发布:翻拍小说的网络电视剧 编辑:程序博客网 时间:2024/06/16 03:57
我们经常会从数据库里读取的数据显示出来,当数据行较多时就需要把数据分页显示。
这里写了一个通用的分页函数:paging.func.php
<?php/** * 数据分页函数 * @param integer $count 数据总行数 * @param integer $showCount 显示行数 * @param integer $index 当前页号 * @param integer $navCount 导航最大项数 * @return array( * 'curIndex' => 当前页号, * 'pageCount' => 总页数, * 'beginIndex' => 当前页开始行, * 'endIndex' => 当前页结束行, * 'count' => 数据行数, * 'nav' => array( * 'curIndex' => 当前页号, * 'beginIndex' => 分页导航开始页, * 'endIndex' => 分页导航结束页, * 'prevIndex' => 上一页, * 'nextIndex' => 下一页, * 'firstIndex' => 首页, * 'lastIndex' => 尾页, * ) * ) */function Paging($count, $showCount, $index, $navCount){ $pageCount = (integer)($count / $showCount); if($count % $showCount){ $pageCount += 1; } $beginIndex = $index * $showCount; $endIndex = $beginIndex + $showCount; if($endIndex > $count){ $endIndex = $count; } $ret = array( 'pageCount' => $pageCount, 'curIndex' => $index, 'beginIndex' => $beginIndex, 'endIndex' => $endIndex, 'count' => $count, ); $beginIndex = $index - (integer)($navCount / 2); if($beginIndex < 0){ $beginIndex = 0; } $endIndex = $beginIndex + $navCount; if($endIndex > $pageCount){ $endIndex = $pageCount; $beginIndex = max(0, $endIndex - $navCount); } $ret['nav'] = array( 'beginIndex' => $beginIndex, 'endIndex' => $endIndex, 'curIndex' => $index, 'prevIndex' => max(0, $index - 1), 'nextIndex' => min($pageCount, $index + 1), 'firstIndex' => 0, 'lastIndex' => $pageCount - 1, ); return $ret;}?>
使用例子:test-paging.php
<?phprequire 'paging.func.php';/** * 随机生成字符串 * @param string $length 字符串长度 */function RandomString($length){ static $range = '0123456789qwertyuiopasdfghjklzxcvbnm'; $rangeLen = strlen($range); $string = ''; for($i = 0 ; $i < $length; ++ $i){ $string .= $range[mt_rand(0, $rangeLen - 1)]; } return $string;}/** * 查询数据 * @param integer $count 数据表行数 */function QueryData($count){ for($i = 0; $i < $count; ++ $i){ $data[$i] = array( 'id' => ($i + 1000), 'name' => RandomString(mt_rand(4, 10)), ); } return $data;}/** * 显示数据表 * @param array $data 数据表 * @param integer $showCount 每页行数 * @param integer $count 总行数 * @param integer $index 当前页号 * @param integer $navCount 导航显示页数 */function showData($data, $showCount, $count, $index, $navCount){ $pag = Paging($count, $showCount, $index, $navCount); echo "<table>"; echo "<tr><th>id</th><th>name</td><tr>"; for($i = $pag['beginIndex']; $i < $pag['endIndex']; ++ $i){ $row = &$data[$i]; echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "</tr>"; } echo "</table>"; echo "</div>"; $nav = &$pag['nav']; if(0 < $pag['curIndex']){ echo " <a href='?index={$nav['firstIndex']}'>首页</a>"; echo " <a href='?index={$nav['prevIndex']}'>上一页</a>"; } for($i = $nav['beginIndex']; $i < $nav['endIndex']; ++ $i){ $showNum = $i + 1; if($i == $index){ echo " <span>{$showNum}</span>"; }else{ echo " <a href='?index={$i}'><b>{$showNum}</b></a>"; } } if($pag['curIndex'] + 1 < $pag['pageCount']){ echo " <a href='?index={$nav['nextIndex']}'>下一页</a>"; echo " <a href='?index={$nav['lastIndex']}'>尾页</a>"; } echo " 总{$pag['pageCount']}页 {$pag['count']}条"; echo "</div>";}$data = QueryData(75);$index = isset($_REQUEST['index']) ? $_REQUEST['index'] : 0;showData($data, 10, 75, $index, 5);?>
测试截图:
阅读全文
0 0
- PHP分页函数
- PHP的分页函数-----
- PHP分页函数
- php分页函数
- PHP页面分页函数
- PHP分页函数
- php分页函数
- php分页函数
- 自定义PHP分页函数
- PHP分页导航函数
- php分页函数
- 自定义PHP分页函数
- PHP 分页实现函数
- PHP分页函数应用
- PHP array_slice() 函数分页
- PHP文章内容分页函数
- php分页函数
- php分页函数
- 51 Nod1416DFS
- LTE实战技巧之下载速率提升
- 零基础Objective-C 第三章--------------面向对象编程的基础知识(1)
- 考研之中科大软院
- 暗时间(读书笔记)
- PHP分页函数
- html制作列表
- 利用CVE-2010-2883漏洞制作pdf马
- #游戏unity-音之国度#战斗系统优化
- PPT文档转换成Word文档
- iOS 自动布局实现四等分且宽高比例是1:1
- Unity Shader入门精要笔记(二):Unity Shader基础
- web之利用python超简单爆破5位密码
- Java语言基础(四)