PHP中常用的分页类总结
PHP的分页程序在校园信息发布系统中的应用

据 库 的 信 息 系 统 , 过 网 络 解 析 程 序 , 形 式 丰 富 的 图 文 资 通 把 料 条理 地 展 现 在 浏 览器 上 。在 这 里 , 络 传 媒 的作 用 就 体 现 网 在 校园信息发布系统 中, 备信 息分类 、 交、 具 提 审批 、 宣 的 外
P P解 析 器 。 H
递 回相 应 信 息 。这 种 静 态 网站 的维 护 工 作 繁 重 , 息 如 有 更 信
新 , 要改动较 多页面 , 护效 率低 , 与互 联 网的发展 已 就 维 其 不 能 同步 。院 校对 信 息 的 更 新 是 动 态 实 时 的 , 须采 用 动 态 必 体系建设信 息发布系统才 能动态维 护和管理 。院校根据 自
部 分 的 逻 辑 处 理 都 放 置 在 服 务 器 端 , 问及 操 作 数 据 库 都 在 访
服 务 器 上 进 行 。该 种 模 式 运 行 基 理 见 图 2 。
校 园信息发布程 序是运行在 网络软硬 件平 台搭 建的运
行 环 境 中 连 接 后 台 数 据 库 和 前 台 显 示 模 块 的 系 统 。 后 台 数 据 库 选 用 性 能先 进 的 My QL数 据 库 , 台 解 析 采 用 P P语 S 前 H
构体系运行模式 是: 用户只要通过 网页浏览器对服务器发出
要 求 , 务 器就 可 以 做 出 相 应 来 进 行 数 据 、 料 的 处 理 , 大 服 资 绝
P HP的分页程序在信息发布系统 中的应用 。
2 校 园信 息 发布 系统 设 计 .
tp5分页的传入参数

tp5分页的传入参数
TP5分页传入参数:
1. currentPage:当前页码。
2. listRows:每页显示多少条记录,默认是15条。
3. flag:是否为异步加载,默认是false,如果true则表示为异步加载,
支持JSON格式数据,用于分页返回数据。
4. page_param:如果flag为false,那么此项可传入false,表示使用pathinfo模式,即形如/index/index/p/3,如果传入数字例如3,则表示使用普通模式,即Rewrite模式,形如/index/index/3.html。
5. path:传入URL路径,例如/index/index,如果设置了此参数,当前
url将变更为设置的路径,并自动识别路径中的$page变量为当前页码,务必以斜杠开头和结尾,例如/index/index/。
6. query:查询参数,例如?id=1&name=test,可在当前页和下一页之间
传递参数。
7. fragment:给当前页面加上标识符号,例如#title。
8. theme:可设置分页视图文件主题,默认是不开启,如果开启,需要
在view目录下创建对应的模版文件,如pages-themedosu.html。
9. vars:设置返回的自定义变量,数组类型,例如['total'=>$total,
'page'=>$page],可以在模版中通过$total和$page访问,以此来实现分
页标示,例如当前页码之类的。
hyperf 框架分页 paginate 的参数

hyperf 框架分页 paginate 的参数(原创实用版)目录1.Hyperf 框架分页概述2.Paginate 类参数介绍3.示例代码及配置说明正文【1.Hyperf 框架分页概述】Hyperf 框架是一款基于 PHP 的高性能 Web 开发框架,提供了许多便利的功能,如分页。
在 Hyperf 框架中,分页功能主要由 Paginate 类实现。
Paginate 类允许开发者方便地对数据进行分页展示,提高了用户体验。
【2.Paginate 类参数介绍】Hyperf 框架的 Paginate 类提供了丰富的参数配置,下面将对这些参数进行详细介绍:- `param`:分页参数,必须是一个数组。
包含以下键:- `page`:当前页码,默认为 1。
- `limit`:每页显示的条数,默认为 10。
- `offset`:数据偏移量,默认为 0。
- `total`:数据总数,默认为 0。
- `prev`:上一页链接,默认为"上一页"。
- `next`:下一页链接,默认为"下一页"。
- `first`:首页链接,默认为"首页"。
- `last`:尾页链接,默认为"尾页"。
- `totalPage`:总页数,默认为 0。
- `currentPage`:当前页码,默认为 1。
- `href`:分页链接,默认为"/page/{{page}}"。
- `template`:分页模板,默认为"Hyperf/Paginate/template".模板文件默认位于"application/view/Hyperf/Paginate"目录下。
- `scope`:分页范围,用于设置分页数据的范围,默认为空数组。
- `with`:关联查询的数据,默认为空数组。
- `field`:字段,用于指定分页数据中的字段,默认为*。
thinkphp数据查询方法总结selectfindgetFieldquery

thinkphp数据查询方法总结selectfindgetFieldquery ThinkPHP是一个开源的PHP框架,它提供了丰富的数据库操作方法,用于查询数据库中的数据。
本文将总结ThinkPHP中的常用的数据查询方法:select、find、getField和query。
1. select方法:select方法是查询数据库中的多条数据,返回的是一个数据集(数组)。
```php$data = M('user')->where('status=1')->select(;```以上代码将查询"status=1"的用户数据,并将结果赋值给$data变量。
2. find方法:find方法是查询数据库中的一条数据,返回的是一个数组。
```php$data = M('user')->where('status=1')->find(;```以上代码将查询"status=1"的第一条用户数据,并将结果赋值给$data变量。
3. getField方法:getField方法是查询数据库中一些字段的值,返回的是该字段的值。
```php$name = M('user')->where('status=1')->getField('name');```以上代码将查询"status=1"的第一条用户数据的name字段的值,并将结果赋值给$name变量。
4. query方法:query方法可以直接执行原生的SQL语句,并返回查询结果。
```php$data = M(->query('SELECT * FROM user WHERE status=1');```以上代码将执行指定的SQL语句,并将结果赋值给$data变量。
PHP网站文件结构

一、文件结构:整个网站目录结构如下:(颜色示例:一级目录二级目录三级目录四级目录)/ 根目录(比如:/home/www/,可能会有所变化。
)|--- Documents(存放开发文档文件)|--- ProjectName1(存放项目1的文档)|--- Demond(需求文档)|--- Design(设计文档)|--- Test(测试文档)|--- Database(数据库文档)|--- Discuss(讨论记录)|--- ProjectName2(存放项目2的文档)|--- …………|--- Other(存放独立于项目之外的文档)|--- Include(存放包含文件)|--- Class(放置类定义文件,每一个类单独一个文件来定义。
)|--- ClassFile1|--- ClassFile2|--- …………|--- Config.inc.php 存储系统参数设置信息|--- Functions.inc.php 公用函数库文件|--- OtherFunction.inc.php 其他特定应用的函数库文件。
根据实际情况命名。
|--- Setup.inc.php 参数变量初始化文件。
其他的程序直接包含这个文件即可。
|--- Html|--- Images(存放网站图片文件,并根据栏目的名称建立相应的子目录)|--- ChannelName1(存放ChannelName1栏目的图片文件)|--- ChannelName2|--- CSS (存放网站样式表文件)|--- Include (存放网页中需要包含的文件,比如JS教本等等。
)|--- ChannelName1 (存放ChannelName1栏目的程序文件。
)|----ChannelName2|--- …………|--- Admin(存放整个网站的管理程序。
这个目录需要通过各种方法来加以保护。
)|-- -Cache(Smarty模板生成的Cache文件,目录权限需要Other可写。
page分页类

page分页类<?php/**file: Page.class.php完美分页类 Page*/class Page {private $total; //数据表中总记录数private $listRows; //每页显⽰⾏数private $limit; //SQL语句使⽤limit从句,限制获取记录个数private $uri; //⾃动获取url的请求地址private $pageNum; //总页数private $page; //当前页private $config = array('head' => "条记录",'prev' => "上⼀页",'next' => "下⼀页",'first'=> "⾸页",'last' => "末页");//在分页信息中显⽰内容,可以⾃⼰通过set()⽅法设置private $listNum = 10; //默认分页列表显⽰的个数/**构造⽅法,可以设置分页类的属性@param int $total 计算分页的总记录数@param int $listRows 可选的,设置每页需要显⽰的记录数,默认为25条@param mixed $query 可选的,为向⽬标页⾯传递参数,可以是数组,也可以是查询字符串格式@param bool $ord 可选的,默认值为true, 页⾯从第⼀页开始显⽰,false则为最后⼀页*/public function __construct($total, $listRows=25, $query="", $ord=true){$this->total = $total-1;$this->listRows = $listRows;$this->uri = $this->getUri($query);$this->pageNum = ceil($this->total / $this->listRows);/*以下判断⽤来设置当前⾯*/if(!empty($_GET["page"])) {$page = $_GET["page"];}else{if($ord)$page = 1;else$page = $this->pageNum;}if($total > 0) {if(preg_match('/\D/', $page) ){$this->page = 1;}else{$this->page = $page;}}else{$this->page = 0;}$this->limit = "LIMIT ".$this->setLimit();}/**⽤于设置显⽰分页的信息,可以进⾏连贯操作@param string $param 是成员属性数组config的下标@param string $value ⽤于设置config下标对应的元素值@return object 返回本对象⾃⼰$this,⽤于连惯操作*/function set($param, $value){if(array_key_exists($param, $this->config)){$this->config[$param] = $value;}return $this;}/* 不是直接去调⽤,通过该⽅法,可以使⽤在对象外部直接获取私有成员属性limit和page的值 */function __get($args){if($args == "limit" || $args == "page")return $this->$args;elsereturn null;}/**按指定的格式输出分页@param int 0-7的数字分别作为参数,⽤于⾃定义输出分页结构和调整结构的顺序,默认输出全部结构@return string 分页信息内容*/function fpage(){$arr = func_get_args();$html[0] = "<span class='p1'> 共<b> {$this->total} </b>{$this->config["head"]} </span>";$html[1] = " 本页 <b>".$this->disnum()."</b> 条 ";$html[2] = " 本页从 <b>{$this->start()}-{$this->end()}</b> 条 ";$html[3] = " <b>{$this->page}/{$this->pageNum}</b>页 ";$html[4] = $this->firstprev();$html[5] = $this->pageList();$html[6] = $this->nextlast();$html[7] = $this->goPage();$fpage = '<div id="fenyea" style="font:12px \'\5B8B\4F53\',san-serif;">';if(count($arr) < 1)$arr = array(0, 1,2,3,4,5,6,7);for($i = 0; $i < count($arr); $i++)$fpage .= $html[$arr[$i]];$fpage .= '</div>';return $fpage;}/* 在对象内部使⽤的私有⽅法,*/private function setLimit(){if($this->page > 0)return ($this->page-1)*$this->listRows.", {$this->listRows}";elsereturn 0;}/* 在对象内部使⽤的私有⽅法,⽤于⾃动获取访问的当前URL */private function getUri($query){$request_uri = $_SERVER["REQUEST_URI"];$url = strstr($request_uri,'?') ? $request_uri : $request_uri.'?';if(is_array($query))$url .= http_build_query($query);else if($query != "")$url .= "&".trim($query, "?&");$arr = parse_url($url);if(isset($arr["query"])){parse_str($arr["query"], $arrs);unset($arrs["page"]);$url = $arr["path"].'?'.http_build_query($arrs);}if(strstr($url, '?')) {if(substr($url, -1)!='?')$url = $url.'&';}else{$url = $url.'?';}return $url;}/* 在对象内部使⽤的私有⽅法,⽤于获取当前页开始的记录数 */private function start(){if($this->total == 0)return 0;elsereturn ($this->page-1) * $this->listRows+1;}/* 在对象内部使⽤的私有⽅法,⽤于获取当前页结束的记录数 */private function end(){return min($this->page * $this->listRows, $this->total);}/* 在对象内部使⽤的私有⽅法,⽤于获取上⼀页和⾸页的操作信息 */private function firstprev(){if($this->page > 1) {$str = " <a href='{$this->uri}page=1'>{$this->config["first"]}</a> ";$str .= "<a href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a> ";return $str;}}/* 在对象内部使⽤的私有⽅法,⽤于获取页数列表信息 */private function pageList(){$linkPage = " <b>";$inum = floor($this->listNum/2);/*当前页前⾯的列表 */for($i = $inum; $i >= 1; $i--){$page = $this->page-$i;if($page >= 1)$linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";}/*当前页的信息 */if($this->pageNum > 1)$linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{$this->page}</span> ";/*当前页后⾯的列表 */for($i=1; $i <= $inum; $i++){$page = $this->page+$i;if($page <= $this->pageNum)$linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";elsebreak;}$linkPage .= '</b>';return $linkPage;}/* 在对象内部使⽤的私有⽅法,获取下⼀页和尾页的操作信息 */private function nextlast(){if($this->page != $this->pageNum) {$str = " <a href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a> ";$str .= " <a href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a> ";return $str;}}/* 在对象内部使⽤的私有⽅法,⽤于显⽰和处理表单跳转页⾯ */private function goPage(){if($this->pageNum > 1) {return ' <input style="width:20px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.va }}/* 在对象内部使⽤的私有⽅法,⽤于获取本页显⽰的记录条数 */private function disnum(){if($this->total > 0){return $this->end()-$this->start()+1;}else{return 0;}}}。
超好用的thinkphp5.0thinkphp5.1分页插件!详细使用步骤(内附代码)

超好⽤的thinkphp5.0thinkphp5.1分页插件!详细使⽤步骤(内附代码)效果tp5.0使⽤⽅法page下载地址: 提取码:s75k1,把page⽂件夹整个⽬录复制到⽬录extend下2,修改默认配置 app/config.php把⾥⾯的'paginate' => ['type' => 'page\Page',//修改这个原始为bootstrap'var_page' => 'page','list_rows' => 5,],3,控制器中 ,参考⼀下$name=$request->param('name');//接受参数⼀定要⽤param接收可以接收post和get$where=[['status','eq',1]];if (!empty($name)){$where[]=['name','like',"%$name%"];}$delList=Db::table('staff')->where($where)->order('id asc')->paginate(4,false,['query'=>$request->param()]);4,视图中{$delList->render()}tp5.1使⽤⽅法1,把page⽂件夹整个⽬录复制到⽬录extend下2,控制器中的⽅法public function index(Request $request){$name=$request->param('name');//接受参数⼀定要⽤param接收可以接收post和get$where=[['status','eq',1]];if (!empty($name)){$where[]=['name','like',"%$name%"];}$list=Db::table('staff')->where($where)->order('id asc')->paginate(12,false,['query'=>$request->param(),'type' => 'page\Page','var_page' => 'page']); $page=$list->render();//paginate第⼀个参数为每页显⽰的条数//paginate第⼆个参数为简单模式或总数//paginate第三个参数为分页的条件也可以写数组//总条数$allData=Db::table('staff')->where('status',1)->count();$this->assign('list',$list);$this->assign('page',$page);$this->assign('count',$allData);return $this->fetch('staff-list');}3,视图中,⼀定要写raw,具体含义看⽂档{$page|raw}Page.php 如下图<?phpnamespace page;// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2017 All rights reserved.// +----------------------------------------------------------------------// | Licensed ( /licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: zhangyajun <448901948@>// +----------------------------------------------------------------------use think\Paginator;class Page extends Paginator{//⾸页protected function home() {if ($this->currentPage() > 1) {return "<a href='" . $this->url(1) . "' title='⾸页'>⾸页</a>";} else {return "<p>⾸页</p>";}}//上⼀页protected function prev() {if ($this->currentPage() > 1) {return "<a href='" . $this->url($this->currentPage - 1) . "' title='上⼀页'>上⼀页</a>";} else {return "<p>上⼀页</p>";}}//下⼀页protected function next() {if ($this->hasMore) {return "<a href='" . $this->url($this->currentPage + 1) . "' title='下⼀页'>下⼀页</a>";} else {return"<p>下⼀页</p>";}}//尾页protected function last() {if ($this->hasMore) {return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>";} else {return "<p>尾页</p>";}}//统计信息protected function info(){return "<p class='pageRemark'>共<b>" . $this->lastPage ."</b>页 <b>" . $this->total . "</b>条数据</p>";}/*** 页码按钮* @return string*/protected function getLinks(){$block = ['first' => null,'slider' => null,'last' => null];$side = 3;$window = $side * 2;if ($this->lastPage < $window + 6) {$block['first'] = $this->getUrlRange(1, $this->lastPage);} elseif ($this->currentPage <= $window) {$block['first'] = $this->getUrlRange(1, $window + 2);$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);} elseif ($this->currentPage > ($this->lastPage - $window)) {$block['first'] = $this->getUrlRange(1, 2);$block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);} else {$block['first'] = $this->getUrlRange(1, 2);$block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);}$html = '';if (is_array($block['first'])) {$html .= $this->getUrlLinks($block['first']);}if (is_array($block['slider'])) {$html .= $this->getDots();$html .= $this->getUrlLinks($block['slider']);}if (is_array($block['last'])) {$html .= $this->getDots();$html .= $this->getUrlLinks($block['last']);}return$html;}/*** 渲染分页html* @return mixed*/public function render(){if ($this->hasPages()) {if ($this->simple) {return sprintf('%s<div class="pagination">%s %s %s</div>',$this->css(),$this->prev(),$this->getLinks(),$this->next());} else {return sprintf('%s<div class="pagination">%s %s %s %s %s %s</div>',$this->css(),$this->home(),$this->prev(),$this->getLinks(),$this->next(),$this->last(),$this->info());}}}/*** ⽣成⼀个可点击的按钮** @param string $url* @param int $page* @return string*/protected function getAvailablePageWrapper($url, $page){return '<a href="' . htmlentities($url) . '" title="第"'. $page .'"页" >' . $page . '</a>';}/*** ⽣成⼀个禁⽤的按钮** @param string $text* @return string*/protected function getDisabledTextWrapper($text){return '<p class="pageEllipsis">' . $text . '</p>';}/*** ⽣成⼀个激活的按钮** @param string $text* @return string*/protected function getActivePageWrapper($text){return '<a href="" class="cur">' . $text . '</a>';}/*** ⽣成省略号按钮** @return string*/protected function getDots(){return$this->getDisabledTextWrapper('...');}/*** 批量⽣成页码按钮.** @param array $urls* @return string*/protected function getUrlLinks(array$urls){$html = '';foreach ($urls as$page => $url) {$html .= $this->getPageLinkWrapper($url, $page); }return$html;}/*** ⽣成普通页码按钮** @param string $url* @param int $page* @return string*/protected function getPageLinkWrapper($url, $page){if ($page == $this->currentPage()) {return$this->getActivePageWrapper($page);}return$this->getAvailablePageWrapper($url, $page); }/*** 分页样式*/protected function css(){return ' <style type="text/css">.pagination p{margin:0;cursor:pointer}.pagination{height:40px;padding:20px 0px;}.pagination a{display:block;float:left;margin-right:10px;padding:2px 12px;height:24px;border:1px #cccccc solid;background:#fff;text-decoration:none;color:#808080;font-size:12px;line-height:24px;}.pagination a:hover{color:#077ee3;background: white;border:1px #077ee3 solid;}.pagination a.cur{border:none;background:#077ee3;color:#fff;}.pagination p{float:left;padding:2px 12px;font-size:12px;height:24px;line-height:24px;color:#bbb;border:1px #ccc solid;background:#fcfcfc;margin-right:8px;}.pagination p.pageRemark{border-style:none;background:none;margin-right:0px;padding:4px 0px;color:#666;}.pagination p.pageRemark b{color:red;}.pagination p.pageEllipsis{border-style:none;background:none;padding:4px 0px;color:#808080;}.dates li {font-size: 14px;margin:20px 0}.dates li span{float:right}</style>';}}-----------------------------------------------------------------------优化如下:。
php 面试知识点整理归纳

php 面试知识点整理归纳PHP面试知识点整理一、PHP基础知识1. PHP的概述和特点:介绍PHP的发展历史、特点以及在Web开发中的应用。
2. PHP的安装和配置:讲解如何安装PHP以及配置PHP的环境变量。
3. PHP的基本语法:介绍PHP的基本语法结构,如变量、数据类型、运算符、流程控制等。
4. PHP的函数和数组:讲解PHP的函数的定义和使用,以及数组的操作。
5. PHP的面向对象编程:介绍PHP的面向对象编程的基本概念和语法,如类、对象、继承、多态等。
二、PHP高级特性1. PHP的命名空间:讲解PHP的命名空间的作用和使用方法。
2. PHP的异常处理:介绍PHP的异常处理机制,如何捕获和处理异常。
3. PHP的自动加载:讲解PHP的自动加载机制,如何自动加载类文件。
4. PHP的魔术方法:介绍PHP的魔术方法,如__construct、__destruct、__get、__set等。
5. PHP的Trait特性:讲解PHP的Trait特性,如何解决多继承的问题。
三、PHP常用扩展1. 数据库扩展:介绍PHP常用的数据库扩展,如MySQL、PDO等,以及它们的使用方法。
2. 文件操作扩展:讲解PHP的文件操作扩展,如文件读写、文件上传等。
3. 图像处理扩展:介绍PHP的图像处理扩展,如GD库的使用。
4. 缓存扩展:讲解PHP的缓存扩展,如Memcached、Redis的使用。
5. 其他常用扩展:介绍PHP的其他常用扩展,如CURL、XML等。
四、PHP性能优化1. 代码优化:介绍PHP代码的优化技巧,如避免内存泄漏、减少数据库查询次数等。
2. 缓存优化:讲解PHP的缓存机制,如页面缓存、数据缓存等。
3. 延迟加载:介绍延迟加载的概念和实现方式,如懒加载、预加载等。
4. 数据库优化:讲解数据库的优化技巧,如索引优化、查询优化等。
5. 高并发优化:介绍PHP的高并发优化方法,如使用队列、分布式缓存等。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
PHP中常用的分页类总结分页是目前在显示大量结果时所采用的最好的方式。
有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据。
在互联网上,分页是一般用于搜索结果或是浏览全部信息php基本分页代码如下复制代码<?php// database connection info$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL",E_USER_ERROR);$db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);// find out how many rows are in the table$sql = "SELECT COUNT(*) FROM numbers";$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);$r = mysql_fetch_row($result);$numrows = $r[0];// number of rows to show per page$rowsperpage = 10;// find out total pages$totalpages = ceil($numrows / $rowsperpage);// get the current page or set a defaultif (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {// cast var as int$currentpage = (int) $_GET['currentpage'];} else {// default page num$currentpage = 1;} // end if// if current page is greater than total pages...if ($currentpage > $totalpages) {// set current page to last page$currentpage = $totalpages;} // end if// if current page is less than first page...if ($currentpage < 1) {// set current page to first page$currentpage = 1;} // end if// the offset of the list, based on current page$offset = ($currentpage - 1) * $rowsperpage;// get the info from the db$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched...while ($list = mysql_fetch_assoc($result)) {// echo dataecho $list['id'] . " : " . $list['number'] . "<br />";} // end while/****** build the pagination links ******/// range of num links to show$range = 3;// if not on page 1, don't show back linksif ($currentpage > 1) {// show << link to go back to page 1echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";// get previous page num$prevpage = $currentpage - 1;// show < link to go back to 1 pageecho " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if// loop to show links to range of pages around current pagefor ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number...if (($x > 0) && ($x <= $totalpages)) {// if we're on current page...if ($x == $currentpage) {// 'highlight' it but don't make a linkecho " [<b>$x</b>] ";// if not current page...} else {// make it a linkecho " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";} // end else} // end if} // end for// if not on last page, show forward and last page linksif ($currentpage != $totalpages) {// get next page$nextpage = $currentpage + 1;// echo forward link for next pageecho " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";// echo forward link for lastpageecho " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";} // end if/****** end build pagination links ******/?>先看一个常用的php分页类代码如下复制代码<?php/*Place code to connect to your DB here.*/include('config.php'); // include your code to connect to DB.$tbl_name=""; //your table name// How many adjacent pages should be shown on each side?$adjacents = 3;/*First get total number of rows in data table.If you have a WHERE clause in your query, make sure you mirror it here.*/$query = "SELECT COUNT(*) as num FROM $tbl_name";$total_pages = mysql_fetch_array(mysql_query($query));$total_pages = $total_pages[num];/* Setup vars for query. */$targetpage = "filename.php"; //your file name (the name of this file)$limit = 2; //how many items to show per page$page = $_GET['page'];if($page)$start = ($page - 1) * $limit; //first item to display on this pageelse$start = 0; //if no page var is given, set start to 0/* Get data. */$sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";$result = mysql_query($sql);/* Setup page vars for display. */if ($page == 0) $page = 1; //if no page var is given, default to 1.$prev = $page - 1; //previous page is page - 1$next = $page + 1; //next page is page + 1$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1/*Now we apply our rules and draw the pagination object.We're actually saving the code to a variable in case we want to draw it more than once.*/$pagination = "";if($lastpage > 1){$pagination .= "<div class="pagination">";//previous buttonif ($page > 1)$pagination.= "<a href="$targetpage?page=$prev">� previous</a>";else$pagination.= "<span class="disabled">� previous</span>";//pagesif ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up {for ($counter = 1; $counter <= $lastpage; $counter++){if ($counter == $page)$pagination.= "<span class="current">$counter</span>";else$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";}}elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some{//close to beginning; only hide later pagesif($page < 1 + ($adjacents * 2)){for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){if ($counter == $page)$pagination.= "<span class="current">$counter</span>";else$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";}$pagination.= "...";$pagination.= "<a href="$etpage?page=$lpm1">$lpm1</a>"; $pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";}//in middle; hide some front and some backelseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)){$pagination.= "<a href="$targetpage?page=1">1</a>";$pagination.= "<a href="$targetpage?page=2">2</a>";$pagination.= "...";for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++){if ($counter == $page)$pagination.= "<span class="current">$counter</span>";else$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";}$pagination.= "...";$pagination.= "<a href="$targetpage?page=$lpm1">$lpm1</a>";$pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";}//close to end; only hide early pageselse{$pagination.= "<a href="$targetpage?page=1">1</a>";$pagination.= "<a href="$targetpage?page=2">2</a>";$pagination.= "...";for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {if ($counter == $page)$pagination.= "<span class="current">$counter</span>";else$pagination.= "<a href="$targetpage?page=$counter">$counter</a>";}}}//next buttonif ($page < $counter - 1)$pagination.= "<a href="$targetpage?page=$next">next �</a>";else$pagination.= "<span class="disabled">next �</span>";$pagination.= "</div>n";}?><?phpwhile($row = mysql_fetch_array($result)){// Your while loop here}?><?=$pagination?>实例代码如下复制代码<?phpclass PageView{/**页码**/public $pageNo = 1;/**页大小**/public $pageSize = 20;/**共多少页**/public $pageCount = 0;/**总记录数**/public $totalNum = 0;/**偏移量,当前页起始行**/public $offSet = 0;/**每页数据**/public $pageData = array();/**是否有上一页**/public $hasPrePage = true;/**是否有下一页**/public $hasNextPage = true;public $pageNoList = array();public $jsFunction ='jsFunction';/**** @param unknown_type $count 总行数* @param unknown_type $size 分页大小* @param unknown_type $string*/public function __construct($count=0, $size=20,$pageNo=1,$pageData=array(),$jsFunction='jsFunction'){$this->totalNum = $count;//总记录数$this->pageSize = $size;//每页大小$this->pageNo = $pageNo;//计算总页数$this->pageCount = ceil($this->totalNum/$this->pageSize);$this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;//检查pageNo$this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;$this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;//计算偏移$this->offset = ( $this->pageNo - 1 ) * $this->pageSize;//计算是否有上一页下一页$this->hasPrePage = $this->pageNo == 1 ?false:true;$this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;$this->pageData = $pageData;$this->jsFunction = $jsFunction;}/*** 分页算法* @return*/private function generatePageList(){$pageList = array();if($this->pageCount <= 9){for($i=0;$i<$this->pageCount;$i++){array_push($pageList,$i+1);}}else{if($this->pageNo <= 4){for($i=0;$i<5;$i++){array_push($pageList,$i+1);}array_push($pageList,-1);array_push($pageList,$this->pageCount);}else if($this->pageNo > $this->pageCount - 4){array_push($pageList,1);array_push($pageList,-1);for($i=5;$i>0;$i--){array_push($pageList,$this->pageCount - $i+1);}}else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){ array_push($pageList,1);array_push($pageList,-1);array_push($pageList,$this->pageNo -2);array_push($pageList,$this->pageNo -1);array_push($pageList,$this->pageNo);array_push($pageList,$this->pageNo + 1);array_push($pageList,$this->pageNo + 2);array_push($pageList,-1);array_push($pageList,$this->pageCount);}}return $pageList;}/**** 创建分页控件* @param* @return String*/public function echoPageAsDiv(){$pageList = $this->generatePageList();$pageString ="<div class='pagination'><div class='page-bottom'>";if(!empty($pageList)){if($this->pageCount >1){if($this->hasPrePage){$pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一页</a>";}foreach ($pageList as $k=>$p){if($this->pageNo == $p){$pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";continue;}if($p == -1){$pageString = $pageString ."<span class='page-break'>...</span>";continue;}$pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p .")">" . $p . "</a>";}if($this->hasNextPage){$pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一页</a>";}}}$pageString = $pageString .("</div></div>");return $pageString;}}?>css代码>y: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}gin-bottom: 20px;}ur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, . k;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; v}tion .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .p ed3d83; color:#e9357d; font-weight:bold;}border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}v_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image:ge.gif);}v { background-position: -0px -38px; padding-left: 16px;}v_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}t { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}t_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}{background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}ak {border: medium none; background:none transparent; color:#333;}在php页面中的调用方法代码如下复制代码$pageNo = $_GET['pageNo'];if(empty($pageNo)){$pageNo = 1;}//分页数据$pageData = News::getNewsPage($pageNo,$pageSize);//取得总行数$count = News::getNewsCount();//创建分页器$p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);//生成页码$pageViewString = $p->echoPageAsDiv();。