最强的php分页类支持thinkphp框架!整个类代码给出并说明
- <?php
- /*
- * 原创代码请保留
- * php资源 http://www.phpzy.com
- */
- class Page{
- // 起始行数
- public $firstRow ;
- // 列表每页显示行数
- public $listRows ;
- // 分页总页面数
- protected $totalPages ;
- // 总行数
- protected $totalRows ;
- // 当前页数
- protected $nowPage ;
- // 分页栏每页显示的页数
- protected $rollPage ;
- // 分页显示定制
- protected $c ;
- /**
- +----------------------------------------------------------
- * 架构函数
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param array $totalRows 总的记录数
- * @param array $listRows 每页显示记录数
- +----------------------------------------------------------
- */
- public function __construct($totalRows,$listRows) {
- $this->totalRows = $totalRows;
- $this->listRows = !emptyempty($listRows)?$listRows:C('PAGE_LISTROWS');
- $this->totalPages = ceil($this->totalRows/$this->listRows); //总页数
- $this->nowPage = !emptyempty($_GET['p'])?(int)$_GET['p']:1;
- if(!emptyempty($this->totalPages) && $this->nowPage>$this->totalPages) {
- $this->nowPage = $this->totalPages;
- }
- $this->firstRow = $this->listRows*($this->nowPage-1);
- }
- public function setC($name,$value) {
- $this->c[$name] = $value;
- }
- /**
- +----------------------------------------------------------
- * 分页显示输出
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- */
- /**
- * multi($mpurl路径,$num总记录, $perpage每页多少条记录, $curpage页码p, $maxpages = 0最大页码, $page = 10(123456789), $simple = FALSE开启上下页)
- */
- function multi($mpurl,$num=100, $perpage=10, $curpage=1, $maxpages = 0, $page = 10, $simple = FALSE)
- {
- if($this->totalRows)$num=$this->totalRows;
- if($this->listRows)$perpage=$this->listRows;
- if($this->nowPage)$curpage= $this->nowPage;
- if($this->c['page'])$page= $this->c['page'];
- $multipage = '';
- $realpages = 1;
- if($num > $perpage)
- {
- $offset = 2;
- $realpages = @ceil($num / $perpage);
- $pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;
- if($page > $pages)
- {
- $from = 1;
- $to = $pages;
- } else
- {
- $from = $curpage - $offset;
- $to = $from + $page - 1;
- if($from < 1)
- {
- $to = $curpage + 1 - $from;
- $from = 1;
- if($to - $from < $page)
- {
- $to = $page;
- }
- } elseif($to > $pages)
- {
- $from = $pages - $page + 1;
- $to = $pages;
- }
- }
- $html='';
- if($this->c['html']) $html=$this->c['html'];
- $d = explode("###", $this->c['dangqianye']);
- $b = explode("###", $this->c['bianhao']);
- if($this->c['shouye']){ $sy = explode("###", $this->c['shouye']);$multipage .= ($curpage - $offset > 1 && $pages > $page ? $sy['0'].$mpurl.'1'.$sy['1'].$html : '');}
- if($this->c['shangyiye']){ $s = explode("###", $this->c['shangyiye']);$multipage .=($curpage > 1 && !$simple ? $s['0'].$mpurl.($curpage - 1).$s['1'].$html: '');}
- for($i = $from; $i <= $to; $i++)
- {
- $multipage .= $i == $curpage ? $d[0].$i.$d[1] : $b[0].'<a href="'.$mpurl.$i.$html.'">'.$i.'</a>'.$b[1];
- }
- if($this->c['xiayiye']){ $x = explode("###", $this->c['xiayiye']); $multipage .= ($curpage < $pages && !$simple ? $x['0'].$mpurl.($curpage + 1).$x['1'].$html : '');}
- if($this->c['moye']){ $m = explode("###", $this->c['moye']); $multipage .=($to < $pages ? $m['0'].$mpurl.$pages.$m['1'].$html : '');}
- }
- return $multipage;
- }
- }
- ?>