-
-
/***
- * php mysql 分頁類
- * 整理 http://bbs.it-home.org
- */
- class Pagination{
- private $_result;
- class Pagination{ 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 = $GLOBALSALS ['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.= '
複製程式碼
2、>複製程式碼
2、呼叫範例
-
-
-
-
-
$page = new Pagination($table, 5, 'page');
- while($row = $ page ->getRecord()){
- echo $row[0],'
';
} echo $page->getPageBar('?',8, 1 ); ?>
| 複製程式碼