分页。php 引用代码
<?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 = ; //默认分页列表显示的个数 /**
构造方法,可以设置分页类的属性
@param int $total 计算分页的总记录数
@param int $listRows 可选的,设置每页需要显示的记录数,默认为25条
@param mixed $query 可选的,为向目标页面传递参数,可以是数组,也可以是查询字符串格式
@param bool $ord 可选的,默认值为true, 页面从第一页开始显示,false则为最后一页
*/
public function __construct($total, $listRows=, $query="", $ord=true){
$this->total = $total;
$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 = ;
else
$page = $this->pageNum;
} if($total > ) {
if(preg_match('/\D/', $page) ){
$this->page = ;
}else{
$this->page = $page;
}
}else{
$this->page = ;
} $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;
else
return null;
} /**
按指定的格式输出分页
@param int 0-7的数字分别作为参数,用于自定义输出分页结构和调整结构的顺序,默认输出全部结构
@return string 分页信息内容
*/
function fpage(){
$arr = func_get_args(); $html[] = "<span class='p1'> 共<b> {$this->total} </b>{$this->config["head"]} </span>";
$html[] = " 本页 <b>".$this->disnum()."</b> 条 ";
$html[] = " 本页从 <b>{$this->start()}-{$this->end()}</b> 条 ";
$html[] = " <b>{$this->page}/{$this->pageNum}</b>页 ";
$html[] = $this->firstprev();
$html[] = $this->pageList();
$html[] = $this->nextlast();
$html[] = $this->goPage(); $fpage = '<div style="font:12px \'\5B8B\4F53\',san-serif;">';
if(count($arr) < )
$arr = array(, ,,,,,,); for($i = ; $i < count($arr); $i++)
$fpage .= $html[$arr[$i]]; $fpage .= '</div>';
return $fpage;
} /* 在对象内部使用的私有方法,*/
private function setLimit(){
if($this->page > )
return ($this->page-)*$this->listRows.", {$this->listRows}";
else
return ;
} /* 在对象内部使用的私有方法,用于自动获取访问的当前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, -)!='?')
$url = $url.'&';
}else{
$url = $url.'?';
} return $url;
} /* 在对象内部使用的私有方法,用于获取当前页开始的记录数 */
private function start(){
if($this->total == )
return ;
else
return ($this->page-) * $this->listRows+;
} /* 在对象内部使用的私有方法,用于获取当前页结束的记录数 */
private function end(){
return min($this->page * $this->listRows, $this->total);
} /* 在对象内部使用的私有方法,用于获取上一页和首页的操作信息 */
private function firstprev(){
if($this->page > ) {
$str = " <a href='{$this->uri}page=1'>{$this->config["first"]}</a> ";
$str .= "<a href='{$this->uri}page=".($this->page-)."'>{$this->config["prev"]}</a> ";
return $str;
} } /* 在对象内部使用的私有方法,用于获取页数列表信息 */
private function pageList(){
$linkPage = " <b>"; $inum = floor($this->listNum/);
/*当前页前面的列表 */
for($i = $inum; $i >= ; $i--){
$page = $this->page-$i; if($page >= )
$linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";
}
/*当前页的信息 */
if($this->pageNum > )
$linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{$this->page}</span> "; /*当前页后面的列表 */
for($i=; $i <= $inum; $i++){
$page = $this->page+$i;
if($page <= $this->pageNum)
$linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";
else
break;
}
$linkPage .= '</b>';
return $linkPage;
} /* 在对象内部使用的私有方法,获取下一页和尾页的操作信息 */
private function nextlast(){
if($this->page != $this->pageNum) {
$str = " <a href='{$this->uri}page=".($this->page+)."'>{$this->config["next"]}</a> ";
$str .= " <a href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a> ";
return $str;
}
} /* 在对象内部使用的私有方法,用于显示和处理表单跳转页面 */
private function goPage(){
if($this->pageNum > ) {
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.value;location=\''.$this->uri.'page=\'+page+\'\'}" value="'.$this->page.'"><input style="cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'page=\'+page+\'\'"> ';
}
} /* 在对象内部使用的私有方法,用于获取本页显示的记录条数 */
private function disnum(){
if($this->total > ){
return $this->end()-$this->start()+;
}else{
return ;
}
}
}
分页。php 引用代码的更多相关文章
- Mvc分页组件MvcSimplePager代码重构
1 Mvc分页组件MvcSimplePager代码重构 1.1 Intro 1.2 MvcSimplePager 代码优化 1.3 MvcSimplePager 使用 1.4 End Mvc分页组件M ...
- Mvc分页组件MvcSimplePager代码重构及使用
1 Mvc分页组件MvcSimplePager代码重构 1.1 Intro 1.2 MvcSimplePager 代码优化 1.3 MvcSimplePager 使用 1.4 End Mvc分页组件M ...
- 实现 iPhone 电子书的分页显示功能的代码
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6650497 原文地址:实现 iPhone 电子书的分页显示功能的代码作者:醉吻 ...
- 分页查询关键代码 多条件查询关键代码 删除选中商品关键代码 修改要先回显再修改 修改要先回显再修改 同一业务集中使用同一servlet的方法
分页查询关键代码: 通过servlet转发回来的各种信息进行分页的设计(转发回的信息有 分页查询的List集合 查询的页码 查询的条数 查询的数据库总条数 查询的总页码) 从开始时循环10次出现十个数 ...
- JQuery Mobile 的引用代码,以及在手机浏览器上字体太小的解决办法
JQuery Mobile 的引用代码: <link rel="stylesheet" href="http://code.jquery.com/mobile/1. ...
- django之分页,纯python代码
Django中分页 py文件代码 """ 自定义分页组件 可以返回分页的数据和分页的HTML代码 """ from django.http ...
- 静态页分页功能js代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 转:PHP分页技术的代码和示例
本文来自:10 Helpful PHP Pagination Scripts For Web Developers 分页是目前在显示大量结果时所采用的最好的方式.有了下面这些代码的帮助,开发人员可以在 ...
- php分页实例附代码
一个典型的PHP分页实例代码分享,学习php的朋友肯定用得到,主要是了解思路: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...
随机推荐
- lucene-SpanNotQuery和SpanOrQuery交迭与全局跨度
1.在匹配结果中排除相互交迭的跨度 SpanNotQuery构造函数的第一个参数表示要包含的跨度对象,第二个参数表示要排除的跨度对象. 1) SpanNearQuery quick_fox=new S ...
- 学习笔记——抽象工厂模式Abstract Factory
在工厂模式的基础上,通过为工厂类增加接口,实现其他产品的生产,而不用一类产品就增加一个工厂. 依然以<真菌世界>游戏故事类比,树作为工厂,如果现在有两类树,一类生产快速弄真菌飞机和20毫米 ...
- Bootstrap学习 - 组件
下拉菜单 注意:需要先引入jQuery.js再引入bootstrap.js(依赖前者) <div class="dropdown pull-right"> //默认就是 ...
- MySQL常用命令总结2
USE db_name; //使用(打开)数据库 SELECT DATABASE(); //查看当前打开的数据库 CREATE TABLE tb_name( column_name data_type ...
- L4,an exciting trip
expressions: a great number of 许多 in the centre of 在…的中部 sentences: I have just had breakfast. I hav ...
- mysql建表: 主键,外键约束
CREATE DATABASE db_studentinfo; USE db_studentinfo ; DROP TABLE IF EXISTS t_student ; CREATE TABLE t ...
- codeforces 369 div2 C dp
http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...
- How to install / setup /upgrade PHP 5.5.x on Ubuntu 12.04 LTS
原文:http://www.dev-metal.com/how-to-setup-latest-version-of-php-5-5-on-ubuntu-12-04-lts/ 最近遇到了要在ubunt ...
- div使用
div style常用属性 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height ...
- JSON & XML 简析
转载自:http://my.oschina.net/aofe/blog/269260 JSON: XML: JSON格式说明: HTML & XML 的对比 HTML: XML: HTML5新 ...