凌的博客

您现在的位置是: 首页 > 学无止境 > PHP > 

PHP

php 分页类(整理)

2015-04-27 PHP 750
/** * 分页类 * @date 2015年4月24日 上午9:31:59 */ class Page{ public $page; //当前页 public $total; //总记录数 public $pagesize; //显示多少分页 public $para
/**
 * 分页类
 * @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);
       }   
}  

文章评论

0条评论