我们在项目开发的过程中避免不了使用分页功能,拿php来说,现在市面上有很多大大小小的php框架,当然了分页这种小功能这些框架中都是拿来直接可以用的。

这些框架的分页功能使用都很方便,配置一下分页所需参数立马就能出结果,对于开发人员来说是非常方便的。但是有些时候就会发现这些分页功能不是自己期望的,

当然拿框架的分页修改一下是可以实现我们的需求的,但是永远局限于框架本身的封装,那么我们怎么样定义自己的分页类呢,那么现在就要求我们不仅要知其然,更要知其所以然,

好了,废话那么多,咱们开始正题。

要实现分页功能,首先要知道数据总条数、每页显示的条数、显示几个分页码,这三个可谓是必要条件。

我们先看一下具体的实现效果

假设表结构是这样

CREATE TABLE `article_information` (

`inf_id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`inf_title` varchar(50) NOT NULL DEFAULT '',

`inf_smtitle` varchar(50) NOT NULL DEFAULT '',

`inf_cid` int(10) unsigned NOT NULL,

`inf_hits` int(10) unsigned NOT NULL DEFAULT '0',

PRIMARY KEY (`inf_id`)

) ENGINE=MyISAM AUTO_INCREMENT=210 DEFAULT CHARSET=utf8;

首先我们连接数据库,php代码为:

$host = '127.0.0.1';$db_user = 'root';$db_pass = 'root';$db_name = 'article';$link = new mysqli($host,$db_user,$db_pass);if($link->errno){printf("数据库链接失败:".mysqli_connect_error());exit();

}if( ! $link->select_db($db_name)){printf("数据库选择失败");exit();

}$count_sql = "SELECT COUNT(1) nums FROM article_information";//查询数据总条数

$query = $link->query($count_sql);$row_count = $query->fetch_assoc();$query->free_result();$total = $row_count['nums'];$page_size = 10;$cur_page = get_url_param(parse_url($_SERVER['REQUEST_URI'])['path'],4);//假如url链接为,http://xxx.net/fenye.php/index/list/6?age=20&city=40

//我这边以平常开发常用的url链接为例,我这个链接只有两层index/list,如果是tp的话无路由链接一般为index/index/list(模块/控制器/方法)

get_url_param函数为:

/**

* 获取url中的分段数据

* @param $url 链接url

* @param $seg 获取url中的第几段*/

function get_url_param($url,$seg){$url = explode('/',$url);return isset($url[$seg]) ? $url[$seg] : 1;

}

接着继续,知道了总条数和每页显示的条数以后,下面我们开始取页码范围内的数据

$start = ($cur_page-1)*$page_size;//按照分页规律计算出数据起始条数

$sql = "SELECT inf_id,inf_title FROM py_information LIMIT $start,$page_size";if ($result = $link->query($sql)){$arr =[];while ($row = $result->fetch_assoc()){$arr[] = $row;

}

}

现在我们已经把数据取出来了,下面我们开始写分页类。

下面是分页类的初始化配置:

具体配置参数我已经注释的很详细了,个别说明一下

$params参数:很多时候我们列表的分页会用到很多的查询参数,我经常看到各个框架论坛里面有同学问怎么把搜索参数带入到分页里面,那么这个参数就是让你把你的筛选参数组合到url中,格式为?id=20&city=30,

下面我们会讲如何将参数组合成这种格式的。

$base_url参数:当前链接(不带参数和页码),例如我们当前访问的地址为:http://xxx.net/fenye.php/index/list/6?age=20&city=40,那么$base_url =/fenye.php/index/list,我这是用原生php写的例子,如果用框架的话相关php文件是可以省略的。

classMypage{private $cur_page;//当前页

private $total;//总条数

private $page_size = 10;//每页显示的条数

private $total_page;//总页数

private $first_page;//首页显示名称

private $pre_page;//上一页的显示名称

private $nex_page;//下一页的显示名称

private $end_page;//尾页名称

private $params;//分页后面的筛选参数

private $num_size = 2;//当前页前后显示几个分页码

private $base_url;//分页链接地址

public function __construct(array $page_config=[])

{$this->cur_page = $page_config['cur_page'];$this->total = $page_config['total'];$this->page_size = $page_config['page_size'];$this->base_url = $page_config['base_url'];$this->pre_page = isset($page_config['pre_page']) ? $page_config['pre_page'] : "上一页";$this->nex_page = isset($page_config['next_page']) ? $page_config['next_page'] : "下一页";$this->end_page = isset($page_config['end_page']) ? $page_config['end_page'] : "尾页";$this->first_page = isset($page_config['first_page']) ? $page_config['first_page'] : "首页";$this->num_size = isset($page_config['num_size']) ? $page_config['num_size'] : 2;$this->params = isset($page_config['params']) ?$page_config['params'] : '';$this->total_page = ceil($this->total/$this->page_size);

}

}

配置好分页类,那么下一步我们就要获取分页码了,像下面图片中的分页样式一样。

我们来看一下一个分页地址需要哪些东西,假如第5页的链接地址为:

5

首先我们要组合第5页的链接地址/fenye.php/index/list/5?age=20&city=40,然后再返回5这个链接,

下面我们来具体实现

获取指定页码的地址:

/**

* 获取分页地址 xxx.com/index/3

* @param $i*/

public function get_url($i){return $this->base_url.'/'.$i;

}

返回整体页码链接,上面我们已经说了分页参数问题,这里$url.=$this->params就是对每一页的url后面都带上分页参数,这样就确保参数的正确性:

/**

* 获取分页完整链接

* @param $url*/

public function get_link($url,$text){if ($this->params) $url.=$this->params;return "$text";

}

有了上面两个函数,现在我们来获取一下第1页的页码链接,下面做了一个小小的判断,我想大家都能看明白,就是当前不是第一页就返回有链接的地址,否则只返回一个链接名称:

/**

* 获取首页的链接地址*/

public functionget_first_page(){if ($this->cur_page > 1 && $this->cur_page != 1){return $this->get_link($this->get_url(1),$this->first_page);

}return "$this->first_page";

}

和上面类似,我们获取一个其它页码的链接地址:

/**

* 获取上一页链接地址*/

public functionget_prev_page(){if ($this->cur_page > 1 && $this->cur_page !=1){return $this->get_link($this->get_url($this->cur_page-1),$this->pre_page);

}return ''.$this->pre_page.'';

}/**

* 获取下一页链接地址

* @return string*/

public functionget_next_page(){if ($this->cur_page < $this->total_page){return $this->get_link($this->get_url($this->cur_page+1),$this->nex_page);

}return ''.$this->nex_page.'';

}/**

* 获取...符号

* @return string*/

public functionget_ext(){return '...';

}/**

* 获取尾页地址*/

public functionget_end_page(){if ($this->cur_page < $this->total_page){return $this->get_link($this->get_url($this->total_page),$this->end_page);

}return ''.$this->end_page.'';

}

还差一点,就是中间的数字页码,注释我已经写得很清楚了:

/**

* 中间的数字分页*/

public functionnow_bar(){if ($this->cur_page > $this->num_size){$begin = $this->cur_page - $this->num_size;$end = $this->cur_page + $this->num_size;//判断最后一页是否大于总页数

if ($end > $this->total_page){//重新计算开始页和结束页

$begin = ($this->total_page - 2*$this->num_size > 0) ? $this->total_page - 2*$this->num_size : 1;//这里为什么用2*$this->num_size呢?因为当前页前后有2个$this->num_size的间距,所以这里是2*$this->num_size

$end = $this->total_page;

}

}else{$begin = 1;$end = 2*$this->num_size+1;//此处的2和上面已经解释过了,+1是因为除了当前页,前后还有2*$this->num_size的间距,所以总页码条数为2*$this->num_size+1

}$page_html='';for ($i=$begin;$i<=$end;$i++){if ($i == $this->cur_page){$page_html .= ''.$i.'';

}else{$page_html .= $this->get_link($this->get_url($i),$i);

}

}return $page_html;

}

其它的两个函数

/**

* 返回总条数

* @return string*/

public functionshow_total_row(){return "共{$this->total}条";

}/**

* 返回总页数*/

public functionshow_total_page(){return "共{$this->total_page}页";

}

最后输出分页:

/**

* 输出分页码*/

public functionshow_page(){$show_page = '';$ext = ($this->cur_page>$this->num_size) ? $this->get_ext() : '';$show_page.= $this->show_total_row();$show_page.= $this->show_total_page();$show_page.= $this->get_first_page();$show_page.= $this->get_prev_page();$show_page.= $ext;$show_page.= $this->now_bar();$show_page.= $ext;$show_page.= $this->get_next_page();$show_page.= $this->get_end_page();return $show_page;

}

以上就是分页的实现原理,下面我会给出具体代码,上面我们说过我们的查询参数组装成?id=20&city=30这种格式的来拼接到url后面,下面我们来看一下具体实现过程。

例如我们把分页条件都放进一个数组中:

$condition =['age'=>20,

'city'=>40];

下面我们来进行url组合:

/**

* 组合url参数 ?id=2&city=3

* @param array $data*/

function make_url(array $data=[]){$link = '?';$suffix = '&';foreach ($data as $key=>$val){$link .= $key.'='.$val.$suffix;

}return trim($link,$suffix);

}

ok参数已经组合成我们所希望的格式了,其实就是一个foreach拼装数据的过程。

以上是一个原理和逻辑,下面给出所有代码:

完整分页类:

* Created by PhpStorm.

* User: 123456

* Date: 2018/9/4

* Time: 17:24*/

classMypage{private $cur_page;//当前页

private $total;//总条数

private $page_size = 10;//每页显示的条数

private $total_page;//总页数

private $first_page;//首页显示名称

private $pre_page;//上一页的显示名称

private $nex_page;//下一页的显示名称

private $end_page;//尾页名称

private $params;//分页后面的筛选参数

private $num_size = 2;//当前页前后显示几个分页码

private $base_url;//分页链接地址

public function __construct(array $page_config=[])

{$this->cur_page = $page_config['cur_page'];$this->total = $page_config['total'];$this->page_size = $page_config['page_size'];$this->base_url = $page_config['base_url'];$this->pre_page = isset($page_config['pre_page']) ? $page_config['pre_page'] : "上一页";$this->nex_page = isset($page_config['next_page']) ? $page_config['next_page'] : "下一页";$this->end_page = isset($page_config['end_page']) ? $page_config['end_page'] : "尾页";$this->first_page = isset($page_config['first_page']) ? $page_config['first_page'] : "首页";$this->num_size = isset($page_config['num_size']) ? $page_config['num_size'] : 2;$this->params = isset($page_config['params']) ?$page_config['params'] : '';$this->total_page = ceil($this->total/$this->page_size);

}/**

* 获取首页的链接地址*/

public functionget_first_page(){if ($this->cur_page > 1 && $this->cur_page != 1){return $this->get_link($this->get_url(1),$this->first_page);

}return "$this->first_page";

}/**

* 获取上一页链接地址*/

public functionget_prev_page(){if ($this->cur_page > 1 && $this->cur_page !=1){return $this->get_link($this->get_url($this->cur_page-1),$this->pre_page);

}return ''.$this->pre_page.'';

}/**

* 获取下一页链接地址

* @return string*/

public functionget_next_page(){if ($this->cur_page < $this->total_page){return $this->get_link($this->get_url($this->cur_page+1),$this->nex_page);

}return ''.$this->nex_page.'';

}/**

* 获取...符号

* @return string*/

public functionget_ext(){return '...';

}/**

* 获取尾页地址*/

public functionget_end_page(){if ($this->cur_page < $this->total_page){return $this->get_link($this->get_url($this->total_page),$this->end_page);

}return ''.$this->end_page.'';

}/**

* 中间的数字分页*/

public functionnow_bar(){if ($this->cur_page > $this->num_size){$begin = $this->cur_page - $this->num_size;$end = $this->cur_page + $this->num_size;//判断最后一页是否大于总页数

if ($end > $this->total_page){//重新计算开始页和结束页

$begin = ($this->total_page - 2*$this->num_size > 0) ? $this->total_page - 2*$this->num_size : 1;//这里为什么用2*$this->num_size呢?因为当前页前后有2个$this->num_size的间距,所以这里是2*$this->num_size

$end = $this->total_page;

}

}else{$begin = 1;$end = 2*$this->num_size+1;//此处的2和上面已经解释过了,+1是因为除了当前页,前后还有2*$this->num_size的间距,所以总页码条数为2*$this->num_size+1

}$page_html='';for ($i=$begin;$i<=$end;$i++){if ($i == $this->cur_page){$page_html .= ''.$i.'';

}else{$page_html .= $this->get_link($this->get_url($i),$i);

}

}return $page_html;

}/**

* 输出分页码*/

public functionshow_page(){$show_page = '';$ext = ($this->cur_page>$this->num_size) ? $this->get_ext() : '';$show_page.= $this->show_total_row();$show_page.= $this->show_total_page();$show_page.= $this->get_first_page();$show_page.= $this->get_prev_page();$show_page.= $ext;$show_page.= $this->now_bar();$show_page.= $ext;$show_page.= $this->get_next_page();$show_page.= $this->get_end_page();return $show_page;

}/**

* 获取分页地址 xxx.com/index/3

* @param $i*/

public function get_url($i){return $this->base_url.'/'.$i;

}/**

* 获取分页完整链接

* @param $url*/

public function get_link($url,$text){if ($this->params) $url.=$this->params;return "$text";

}/**

* 返回总条数

* @return string*/

public functionshow_total_row(){return "共{$this->total}条";

}/**

* 返回总页数*/

public functionshow_total_page(){return "共{$this->total_page}页";

}

}

前台页面:

}if ( ! $link->select_db($db_name)){printf("你选择的数据库{$db_name}不存在");exit();

}$page_size = 10;//p($url);

//p(__DIR__);

//p(__FILE__);

//p(pathinfo(__FILE__, PATHINFO_BASENAME));

$condition =['age'=>20,

'city'=>40];//p(make_url($condition));

//$cur_page = isset($_GET['page']) ? $_GET['page'] : 1;

$cur_page = get_url_param(parse_url($_SERVER['REQUEST_URI'])['path'],4);//假如url链接为,http://xxx.net/fenye.php/index/list/6?age=20&city=40

//我这边以平常开发常用的url链接为例,我这个链接只有两层index/list,如果是tp的话无路由链接一般为index/index/list(模块/控制器/方法)

$sql = "SELECT COUNT(1) nums FROM py_information";$query = $link->query($sql);$row = $query->fetch_assoc();$total = $row['nums'];//数据总条数

$page_config=['cur_page'=>$cur_page,

'total'=>$total,

'page_size'=>$page_size,

'base_url'=>'/fenye.php/index/list',

'num_link'=>2,

'params'=>make_url($condition)

];$mypage = new Mypage($page_config);$start = ($cur_page-1)*$page_size;//按照分页规律计算出数据起始条数

$sql = "SELECT inf_id,inf_title FROM article LIMIT $start,$page_size";if ($result = $link->query($sql)){$arr =[];while ($row = $result->fetch_assoc()){$arr[] = $row;

}

}/**

* 获取url中的分段数据

* @param $url 链接url

* @param $seg 获取url中的第几段*/

function get_url_param($url,$seg){$url = explode('/',$url);return isset($url[$seg]) ? $url[$seg] : 1;

}/**

* 组合url参数 ?id=2&city=3

* @param array $data*/

function make_url(array $data=[]){$link = '?';$suffix = '&';foreach ($data as $key=>$val){$link .= $key.'='.$val.$suffix;

}return trim($link,$suffix);

}function p($data){echo '

';print_r($data);echo '

';

};?>

充值

.page_nav { font-family: Simsun; line-height:normal;text-align: right;margin-top: 10px;overflow: hidden;zoom: 1;text-align:center}.page_nav a,.page_nav span,.page_nav input{display:inline-block;line-height:23px;padding:0 10px;border:1px solid #ccc;background-color:#fff; text-decoration:none;color:#666;margin-right:5px;zoom: 1;}

.page_nav input{height: 23px;line-height: 23px;padding: 0;zoom: 1; font:12px/16px;font-family: Simsun;zoom: 1;_margin-bottom:-4px;}.page_nav a:hover,.page_nav span.pg_curr{color:#fff !important;background-color:#C00;border-color:#be0d11 #be0d11 #9a0307; text-decoration:none;}

.disabled{ background: #C00 !important; color: #fff !important;}

id标题

<?php foreach($arr as $key=>$val):?>

<?php echo $val['inf_id'];?><?php echo $val['inf_title'];?>

=$mypage->show_page();?>

你可以将此分页类移植到tp或者CI等其它框架中去。

数据库你可以随便找一个,这里我就不提供了,感谢大家的阅读,有不对的地方请指正。

另外我在github上面开源了一些项目,觉得有用的给个star,地址:https://github.com/sunjiaqiang

php mysql分页_PHP+Mysql实现分页相关推荐

  1. php mysql mysql_set_charset()._PHP:MySQL函数mysql_set_charset()的用法

    mysql_set_charset (PHP 5 >= 5.2.3) mysql_set_charset - 设置客户端的字符集 本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除.应使用 ...

  2. php加mysql分页_php mysql 分页函数

    一款超简单的php mysql 分页,也是很实例的一款函数,他可以自动获取用户增加的参数,而不需要用户来增加,很自能化的一款自能分页程序. ------------------------------ ...

  3. mysql页码_PHP+MySQL实现输入页码跳转到指定页面功能示例

    本文实例讲述了php+mysql实现输入页码跳转到指定页面功能.分享给大家供大家参考,具体如下: 一.代码 conn.php: $id=mysql_connect("localhost&qu ...

  4. mysql+基本代码_PHP+MySQL扎实基本功十句话_php

    2.写程序前看看怎么用error_reporting. 3.不懂就问本身没错,但你需要在那之前查查手册. 4.当然,你需要懂得使用手册.手册上找不到答案的时候,应该考虑下网络上的搜索引擎. 5.刚学会 ...

  5. php mysql预处理_PHP MySQL 预处理语句

    预处理语句对于防止 MySQL 注入是非常有用的. 预处理语句及绑定参数 预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. 预处理语句的工作原理如下:预处理:创建 SQL 语句模板并发送 ...

  6. php mysql 降_PHP,MySQL:mysql替代php in_array函数

    假如我有一个数组并且我想检查一个元素是否是该数组的一部分,我可以继续使用in_array(needle,haystack)来确定结果.我试图看到PHP相当于我的目的.现在你可能有一个即时的答案,你可能 ...

  7. php mysql购物车_php mysql购物车实现程序

    php mysql购物车实现程序 (2015-05-10 21:31:00) 标签: 股票 分类: PHP 简单容易理解.cookie存购物车ID,db存购物车数据. http://www.111cn ...

  8. php过滤特殊字符mysql攻击_php – MySQL在特殊字符处切断字符串

    我正在尝试使用 PHP将远程POST数据(iSnare发送的文章)插入MySQL.数据来自远程POST发件人,我可以将其写入纯文本文件而不会出现问题. 不幸的是,当它将它插入MySQL时,MySQL会 ...

  9. php mysql 分类_php+mysql实现无限分类实例详解

    本文实例讲述了php+mysql实现无限分类的方法.分享给大家供大家参考.具体分析如下: 1.数据库通过设置父类ID来进行唯一索引,然后使用函数的递归调用实现无限分类: 2.数据库设计通过特定格式进行 ...

  10. php mysql无限_php+mysql实现无限分类实例详解

    php+mysql实现无限分类实例详解 fenlei($arr[$i][0]);   //$arr[$i][1]表示第$i+1个分类的id的值.进行递归,也就是把自己的id作为f_id参数把自己的子类 ...

最新文章

  1. 机器学习读书笔记(一)
  2. MyCAT-1.4-RC性能测试(初步施工)
  3. 简述C/S和B/S模式的区别
  4. string的内存管理问题
  5. 电源纹波分析及测试方法
  6. 占用系统资源测试_高频性能测试面试题15道
  7. python截图直接在内存里调用_Python-按块上传FTP中的内存文件(由API调用生...
  8. 浅谈.net事件机制
  9. 爬虫原理与数据抓取----- Requests模块
  10. ArrayList详细
  11. Structs2-Action
  12. 浅析城市综合管廊配电结构
  13. Package inputenc Error: Unicode character , (U+FFØC) (inputenc) not set up for use with L aTeX. See
  14. 唐玄奘:不要因为走得太远,而忘了为什么出发
  15. whistle-安卓手机配置代理
  16. [Redis] Redis实战
  17. Vue 移动端调用相机和相册实现图片上传
  18. Keil5 平台 S3C2440裸机程序开发-----中断系统/UART
  19. MySQL进阶垫脚石:线程长时间处于killed状态怎么破?
  20. SpringMVC基础--spring MVC配置详解

热门文章

  1. mysql菜鸟手迹1--安装及目录介绍
  2. socket 和 TCP/IP 协议的关系
  3. 解决windows写Django项目在templates中的html文件中引入外部css,js不成功的方法
  4. Spring事务管理实现方式之编程式事务与声明式事务详解
  5. Day3-字符串-数组-正则表达式
  6. docker-compose简单使用
  7. 程序入口地址的直接定制表【 (1) 清屏(2) 设置前景色 (3) 设置背景色 (4) 向上滚动一行】...
  8. Asp.Net 4.0 SEO增强之 UrlRouting
  9. Enterprise Library2.0(1):Data Access Application Block学习
  10. Linux 内存管理之 SLUB分配器(2) :kmalloc_cache 结构