-
-
/***
- * php mysql 分页类
- * 整理 http://bbs.it-home.org
- */
- class Pagination{
- private $_result;
- private $_count; //记录数
- private $_pageMax; //最大页
- private $_page; //当前页
- private $_url;
- private $_startPage;//分页条起码
- private $_endPage; // 分页条止码
- private $_nextPage; //上一页
- private $_prePage; //下一页
function __construct($table, $pageSize, $getPage){
- $this->_result = $GLOBALS['db']->query('SELECT * FROM '.$table);
- $this->_count = $GLOBALS['db']->getRecordNum();
- $this->_pageMax = ceil($this->_count/$pageSize);
- $this->_result = '';
if($_GET[$getPage] == '')
- $this->_page = 1;
- else
- $this->_page= max(intval($_GET[$getPage]), 1);
-
- $this->_page = min($this->_page, $this->_pageMax);
$offset = ($this->_page - 1) * $pageSize;
- $sql = 'SELECT * FROM '.$table.' LIMIT '. $offset .','. $pageSize;
- $this->_result = $GLOBALS['db']->query($sql);
- }
-
- function getRecord(){
- return $GLOBALS['db']->getRecord();
- }
-
- function getPageBar($url = '?', $barLn = 10, $style = 1){
- if($style == 1){
- if($barLn % 2 != 0 ){
- $midder = ceil($barLn / 2);
- $big_repair = $midder - 1 ;//当上面以进一法取整,则这里为减1,反之为加1
- }else{
- $midder = $big_repair = $barLn / 2;
- }
- $sml_repair = $midder- 1;
-
- $this->_startPage = ($this->_page + $midder) > $this->_pageMax ? $this->_pageMax - $barLn : $this->_page - $sml_repair;
- $this->_endPage = $this->_page _page + $big_repair;
- }elseif($style == 2){
-
- if($this->_page % $barLn == 0){
- $this->_startPage = $this->_page;
- }else{
- $this->_startPage = ($this->_page > $barLn)? $this->_page - ($this->_page % $barLn ) : 1;
- }
- $this->_endPage = $this->_startPage + $barLn - 1;
- }
- $this->_url = $url;
- $this->_nextPage = $this->_page + 1;
- $this->_prePage = $this->_page - 1;
- $this->_startPage = max($this->_startPage, 1);//至少从第一页开始
- $this->_endPage = min($this->_endPage, $this->_pageMax);//最多只到末页
-
-
- $this->_result = '当前是:'.$this->_page.'/'.$this->_pageMax.'页,共'.$this->_count.'条记录';
-
- if ($this->_page > 1)
- $this->_result .= '
- 9
- 3
-
';
- else
- $this->_result .= '9
- 3';
-
- for($i = $this->_startPage; $i _endPage; $i++) {
- if ($this->_page == $i)
- $this->_result .= ''.$i.'';
- else
- $this->_result.= ''.$i.'';
- }
-
- if ($this->_page != $this->_pageMax) {
- $this->_result .= '4';
- $this->_result .= ':';
- } else {
- $this->_result.= '4:';
- }
-
- $this->_result.= '
- ';
- return $this->_result;
- }
- }
- ?>
-
复制代码
2、调用示例
-
-
$page = new Pagination($table, 5, 'page');
- while($row = $page->getRecord()){
- echo $row[0],'
';
- }
- echo $page->getPageBar('?',8, 1);
- ?>
复制代码
|