前几篇(上传,缩略图,验证码,自动验证表单)文章介绍的功能实现都是基于ThinkPHP框架封装好的类进行实现的,所以这次自己写一个分页类在框架中使用。

首先在根目录建一个Tools文件夹,在Tools文件夹下建Page.class.php类文件,这样以后自定义的工具类都可放在Tools文件夹下。

此类封装有以下函数:获取请求地址,开始页,从哪一条显示,结束页 从哪一条结束,页码列表(首页超链接,上一页,页码数字列表超链接,下一页,尾页,跳转),对于分页足够使用!

下面是Page.class.php代码

<?php
//命名空间的名称与上级目录tools一致
//原因:当前Page.class.php类文件会被自动加载机制引入
// 在引入的同时会把"tools"变为文件的上级目录,进而获得该Page类文件
namespace Tools; class Page {
private $total; //数据表中总记录数
private $listRows; //每页显示行数
private $limit;
private $uri; //当前链接URL
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;
} 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;';
} //页码列表
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; } }

控制器代码:

//商品列表
function showlist(){ //实现分页效果
$goods = D('goods'); //① 获得数据的总记录条数
$total = $goods -> count(); //select count(*) from sw_goods;
$per = 7; //每页显示7条记录 //②实例化分页类
$page_obj = new \Tools\Page($total, $per); //③自定义sql语句,获取每页信息
$sql = "select * from sw_goods order by goods_id desc ".$page_obj->limit;
$info = $goods->query($sql); //④获取页码列表
$pagelist = $page_obj->fpage(array(3,4,5,6,7,8)); //分配
$this->assign('pagelist',$pagelist);
$this->assign('info',$info);
$this->display();
}

前台显示代码

<{$pagelist}>

  

使用ThinkPHP实现分页功能的更多相关文章

  1. ThinkPHP 分页功能梳理

    最近在开发一个项目,使用了国内流行的ThinkPHP框架,我之前没怎么用过这个框架,也是临时抱佛脚,用的不怎么样?可能理解不是很深刻,如果有说的不对或不正确的地方,请大家多包涵,多指教. ThinkP ...

  2. 简单封装分页功能pageView.js

    分页是一个很简单,通用的功能.作为一个有经验的前端开发人员,有义务把代码中类似这样公共的基础性的东西抽象出来,一来是改善代码的整体质量,更重要的是为了将来做类似的功能或者类似的项目,能减少不必要的重复 ...

  3. php对文本文件进行分页功能简单实现

    php对文本文件进行分页功能简单实现 <!DOCTYPE> <html> <head> <meta http-equiv="Content-type ...

  4. Asp.net MVC3表格共用分页功能

    在建立的mvc3项目中,在Razor(CSHTML)视图引擎下,数据会在表格中自动的生成,但分页没有好的控件实现,这里我们开发了设计了一个分页的模板,适合于没有数据提交和有数据提交的分页的分页. 第一 ...

  5. WinForm DataGridView分页功能

    WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件  .CS: 1 using System; ...

  6. Net 分页功能的实现

    首先写一个接口   1 2 3 4 5 6 public interface IPagedList     {         int CurrentPageIndex { get; set; }   ...

  7. 自己封装的JS分页功能[用于搭配后台使用]

    * 2016.7.03 修复bug,优化代码逻辑 * 2016.5.25 修复如果找不到目标对象的错误抛出. * 2016.5.11 修复当实际页数(pageNumber)小于生成的页码间隔数时的bu ...

  8. 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架

    问题:3行代码 PDF.NET是一个开源的数据开发框架,它的特点是简单.轻量.快速,易上手,而且是一个注释完善的国产开发框架,受到不少朋友的欢迎,也在我们公司的项目中多次使用.但是,PDF.NET比起 ...

  9. jPList – 实现灵活排序和分页功能的 jQuery 插件

    jPList 是一个灵活的 jQuery 插件,可以用于任何 HTML 结构的排序,分页和筛选.它支持的数据源包括:PHP + MySQL,ASP.NET + SQL Server,PHP + SQL ...

随机推荐

  1. postgresql-tps

    tps TPS就是每秒事务数,但是事务是基于虚拟用户数的,假如1个虚拟用户在1秒内完成1笔事务,那么TPS明显就是1:如果 某笔业务响应时间是1ms,那么1个用户在1秒内能完成1000笔事务,TPS就 ...

  2. js变量提升

    JavaScript的函数定义有个特点,它会先扫描整个函数体的语句,把所有申明的变量“提升”到函数顶部: 'use strict'; function foo() { var x = 'Hello, ...

  3. Kafka连接SparkStreaming的两种方式

    第一种方式代码: import org.apache.spark.storage.StorageLevel import org.apache.spark.{HashPartitioner, Spar ...

  4. C#:使用ListView动态添加数据一直闪烁的解决办法

    首先,自定义一个类ListViewNF,继承自 System.Windows.Forms.ListView using System; using System.Collections.Generic ...

  5. ASP.NET Core 1.0 Configuration 配置管理

    documentation: https://docs.asp.net/en/latest/fundamentals/configuration.html github: https://github ...

  6. 分享一套简单的CodeSmith三层模板

    如果要连接mysql,需要安装驱动: https://cdn.mysql.com//Downloads/Connector-Net/mysql-connector-net-8.0.12.msi 连接字 ...

  7. 全网最详细的Windows系统里PLSQL Developer 64bit安装之后的一些配置(图文详解)

    不多说,直接上干货! 注意的是: 本地若没有安装Oracle服务端,Oracle server服务端64位,是远程连接,因此本地配置PLSQL Developer64位. PLSQL Develope ...

  8. 深入聊聊Java多线程

    一.背景 在没有学习Java多线程以前,总觉得多线程是个很神秘的东西,只有那些大神才能驾驭,新年假期没事就来学习和了解一下Java的多线程,本篇博客我们就来从头说一下多线程到底是怎么回事. 二.概述 ...

  9. mycat中间件--linux安装mycat1.6版本

    一.mycat安装前准备1.mycat下载地址,点击此处进行下载2.环境要求如下: mycat使用Java开发,因为用到了JDK 7的部分功能,所以在使用前请确保安装了JDK 7.0,并设置了正确的J ...

  10. 解决wamp 3.0.6 访问路径出现 403 错误

    <VirtualHost *:80> ServerName localhost DocumentRoot e:/wamp64/www #your local dir path <Di ...