Page.class.php :

<?php
namespace Tools; class Page
{
private $total; //数据表中总记录数
private $listRows; //每页显示行数
private $limit;
private $uri;
private $pageNum; //页数
private $config = array('header' => "个记录", "prev" => "上一页", "next" => "下一页", "first" => "首 页", "last" => "尾 页");
private $listNum = 8;
/*
* $total
* $listRows
*/
public function __construct($total, $listRows = 10, $pa = "")
{
$this->total = $total;
$this->listRows = $listRows;
$this->uri = $this->getUri($pa);
$this->page = !empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum = ceil($this->total / $this->listRows);
$this->limit = $this->setLimit();
} private function setLimit()
{
return "Limit " . ($this->page - 1) * $this->listRows . ", {$this->listRows}";
} private function getUri($pa)
{
$url = $_SERVER["REQUEST_URI"] . (strpos($_SERVER["REQUEST_URI"], '?') ? '' : "?") . $pa;
$parse = parse_url($url); if (isset($parse["query"])) {
parse_str($parse['query'], $params);
unset($params["page"]);
$url = $parse['path'] . '?' . http_build_query($params); } return $url;
} public function __get($args)
{
if ($args == "limit") {
return $this->limit;
} else {
return null;
} } private function start()
{
if ($this->total == 0) {
return 0;
} else {
return ($this->page - 1) * $this->listRows + 1;
} } private function end()
{
return min($this->page * $this->listRows, $this->total);
} private function first()
{
$html = "";
if ($this->page == 1) {
$html .= '';
} else {
$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>&nbsp;&nbsp;";
} return $html;
} private function prev()
{
$html = "";
if ($this->page == 1) {
$html .= '';
} else {
$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->page - 1) . "'>{$this->config["prev"]}</a>&nbsp;&nbsp;";
} return $html;
} private function pageList()
{
$linkPage = ""; $inum = floor($this->listNum / 2); for ($i = $inum; $i >= 1; $i--) {
$page = $this->page - $i; if ($page < 1) {
continue;
} $linkPage .= "&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;"; } $linkPage .= "&nbsp;{$this->page}&nbsp;"; for ($i = 1; $i <= $inum; $i++) {
$page = $this->page + $i;
if ($page <= $this->pageNum) {
$linkPage .= "&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
} else {
break;
} } return $linkPage;
} private function next()
{
$html = "";
if ($this->page == $this->pageNum) {
$html .= '';
} else {
$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->page + 1) . "'>{$this->config["next"]}</a>&nbsp;&nbsp;";
} return $html;
} private function last()
{
$html = "";
if ($this->page == $this->pageNum) {
$html .= '';
} else {
$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->pageNum) . "'>{$this->config["last"]}</a>&nbsp;&nbsp;";
} return $html;
} private function goPage()
{
return '&nbsp;&nbsp;<input 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 . '" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'">&nbsp;&nbsp;';
}
public function fpage($display = array(0, 1, 2, 3, 4, 5, 6, 7, 8))
{
$html[0] = "&nbsp;&nbsp;共有<b>{$this->total}</b>{$this->config["header"]}&nbsp;&nbsp;";
$html[1] = "&nbsp;&nbsp;每页显示<b>" . ($this->end() - $this->start() + 1) . "</b>条,本页<b>{$this->start()}-{$this->end()}</b>条&nbsp;&nbsp;";
$html[2] = "&nbsp;&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;&nbsp;"; $html[3] = $this->first();
$html[4] = $this->prev();
$html[5] = $this->pageList();
$html[6] = $this->next();
$html[7] = $this->last();
$html[8] = $this->goPage();
$fpage = '';
foreach ($display as $index) {
$fpage .= $html[$index];
} return $fpage; } }

使用例子:

 $goods = D("Goods");

// 1)分页设置
$total = $goods->count(); // 总记录数
$listRows = 7; // 每页显示条数 // 2)实例化分页对象
$page = new \Tools\Page($total, $listRows); // 3)分页 sql 语句
$sql = "select * from sw_goods order by goods_id desc " . $page->limit;//limit 方法返回'limit xx,xx'
$info = $goods->query($sql); // 4)制作页码列表
$fpage = $page->fpage(array(3,4,5,6,7,8)); // 前面的统计信息不要 // 5)分配数组
$this->assign("info", $info);
$this->assign("pagelist", $fpage);
$this->display("showgoodslist");

 

---->>>

php : 自定义分页类的更多相关文章

  1. Django 自定义分页类

    分页类代码: class Page(object): ''' 自定义分页类 可以实现Django ORM数据的的分页展示 输出HTML代码: 使用说明: from utils import mypag ...

  2. thinkphp自定义分页类

    先来看下这个分页的样式,没写css,确实丑 什么时候写样式再来上传下css吧...... 就是多一个页面跳转功能 先把这个代码贴一下 <?php namespace Component; cla ...

  3. PHP24 自定义分页类

    分页类的定义 <?php /** * Class MyPage 分页类 * @package core */ class MyPage { private $totalCount; //数据表中 ...

  4. python 全栈开发,Day115(urlencode,批量操作,快速搜索,保留原搜索条件,自定义分页,拆分代码)

    今日内容前戏 静态字段和字段 先来看下面一段代码 class Foo: x = 1 # 类变量.静态字段.静态属性 def __init__(self): y = 6 # 实例变量.字段.对象属性 # ...

  5. python---django中自带分页类使用

    请先看在学习tornado时,写的自定义分页类:思路一致: python---自定义分页类 1.基础使用: 后台数据获取: from django.core.paginator import Pagi ...

  6. [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)

    简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...

  7. 好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字

    在做网站没用 JS UI控件时 很实用 用法: var ps=new PageString(); /*可选参数*/ ps.SetIsEnglish = true;// 是否是英文 (默认:false) ...

  8. TP5 分页类,自定义样式

    结合X-admin 后台框架 在做项目,为了保持分页风格的一致,所以需要自定义 一个分页类. 一.在项目的 extend 目录,创建 cus 目录 二.创建 Page 分页类,代码如下 <?ph ...

  9. ThinkPHP3验证码、文件上传、缩略图、分页(自定义工具类、session和cookie)

    验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginCont ...

随机推荐

  1. python引用py文件中文报错

    文件 a.py 中引用文件 b.py 如果文件b.py中包含中文,会报错. 文件hello.py中代码如下: def say_nihao(): print "你好" 文件main. ...

  2. 日志时间格式有s,ms,us,如何排序最大10行

    这个比较繁琐,谁有更好方法?告诉我  [root@module tmp]# cat oldboy.txt       12s120001ms12000000us13s[root@module tmp] ...

  3. CSS中可以和不可以继承的属性

    一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定添加到文本的装饰 text-shad ...

  4. SQLServer中系统存储过程sp_spaceused

    sp_spaceused 执行sp_spaceused存储过程的时候可以不用带参数,直接执行,或者exec sp_spaceused都可以,返回两个结果集: 列名 数据类型 描述 database_n ...

  5. c# 利用动态库DllImport("kernel32")读写ini文件(提供Dmo下载)

    c# 利用动态库DllImport("kernel32")读写ini文件 自从读了设计模式,真的会改变一个程序员的习惯.我觉得嘛,经验也可以从一个人的习惯看得出来,看他的代码编写习 ...

  6. escape(), encodeURI()和encodeURIComponent()(转)

      escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学 ...

  7. Android: 实例解析Activity生命周期

    Activity生命周期图: 下面以一个实例来解析,实例APP运行,进入MainActivity, 点击Send Button以后进入MessgaeActivity 当第一次运行App,进入MainA ...

  8. xml 的 CDATA

    <PolicyURL><![CDATA[http://ectp.tpi.cntaiping.com/TPEBizWeb/pages/B2C/show/downLoad.do?poli ...

  9. SSO之CAS单点登录实例演示

    本文目录: 一.概述 二.演示环境 三.JDK安装配置 四.安全证书配置 五.部署CAS-Server相关的Tomcat 六.部署CAS-Client相关的Tomcat 七. 测试验证SSO 一.概述 ...

  10. 分享一个很早之前写的小工具DtSpyPlus

    几年前写的一个获取windows窗体基本信息和屏幕取色的小工具 ,一直在用. 下载地址 http://files.cnblogs.com/dint/SpyPlus.zip