php常用分页类,php 分页类(整理)

/**

* 分页类

* @date 2015年4月24日 上午9:31:59

*/

class Page{

public $page;//当前页

public $total;//总记录数

public $pagesize;//显示多少分页

public $params;//每页的记录数

public $p;

/**

* @param int $page

* @param int $total

* @param int $pagesize

* @param array $params

*/

public function __construct($page,$total,$pagesize,$params=array(),$p='page') {

$this->page = $page;

$this->pagesize = $pagesize;

$this->total = $total;

$this->params = !empty($params) ? $params : $_GET;

$this->p = $p;

}

/**

* 获取分页链接

* */

public function pageInfo($catid,$type=0){ // catid

//总页数

$pagecount = ceil($this->total/$this->pagesize);

//1

$this->page = $this->page >0 ? $this->page : 1; //当前页

//2

$this->page = $this->page > $pagecount ? $pagecount : $this->page;

//下一页

$next = $this->page > $pagecount ? $pagecount : $this->page+1;

//上一页

$prev = $this->page < 0 ? 1 : $this->page-1;

//获取分页类型

$pattern = D()->field('listnamerule')->from('sort')->where( "id = '{$catid}'" )->getOne();

$url_pattern = $pattern['listnamerule'];

$page = array(

'current' => $this->parsePageLink($this->page,$catid,$url_pattern,$type),

'first' => $this->parsePageLink(1,$catid,$url_pattern,$type),

'prev' => $this->parsePageLink($prev,$catid,$url_pattern,$type),

'next' => $this->parsePageLink($next,$catid,$url_pattern,$type),

'last' => $this->parsePageLink($pagecount,$catid,$url_pattern,$type),

);

//分页列表 1235678

$len = 6;

$len_half = ceil($len/2);

$pages = array();

if( $pagecount>$len ){

if($this->page > $len){

$start = $this->page-$len_half;

if($this->page + $len_half > $pagecount){

$end = $pagecount;

$start = $pagecount-$len+1;

}else{

$end = $this->page + $len_half-1;

}

$pages = range($start,$end);

}else{

$pages = range(1,$len);

}

}else{

$pages = range(1,$len);

}

$page['links'] = array();

if(!empty($pages)){

foreach($pages as $v){

$page['links'][] = $this->parsePageLink($v,$catid,$url_pattern,$type);

}

}

return $page;

}

/**

* 处理分类 分页链接

* @param int $page

*/

public function parsePageLink($page,$catid,$pattern,$type=0){

switch($type){

case 0:

return $this->parseSimpleLink($page);

break;

case 1:

return $this->parseStaticIndexLink($page,$catid);

break;

case 2:

return $this->parseStaticLink($page,$catid,$pattern);

break;

default:

break;

}

}

/**

* 动态链接

* */

public function parseSimpleLink($page){

$this->params[$this->p] = $page;

return http_build_query($this->params);

}

/**

* 静态链接链接 1

* */

public function parseStaticIndexLink($page,$catid){

return str_replace(array('(tid)','(page)'),array($catid,$page),'index_(page).html');

}

/**

* 静态链接链接 2

* */

public function parseStaticLink($page,$catid,$pattern){

return str_replace(array('(tid)','(page)'),array($catid,$page),$pattern);

}

}

如果本文对您有所帮助,请支持下本站哦!!!^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值