1、分页类代码
<?php
/***
* php mysql 分页类
* 整理 http://www.jb200.com
*/
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 < $midder ? $barLn : $this->_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 = '当前是:<font color="#FF0000">'.$this->_page.'</font>/'.$this->_pageMax.'页,共<font color="#FF0000">'.$this->_count.'</font>条记录';
if ($this->_page > 1)
$this->_result .= '<a href="'.$this->_url.'page=1">
<font style="font-family:webdings">9</font></a><a href="'.$this->_url. 'page='.$this->_prePage.'">
<font style="font-family:webdings">3</font>
</a>';
else
$this->_result .= '<font style="font-family:webdings">9</font>
<font style="font-family:webdings">3</font>';
for($i = $this->_startPage; $i <= $this->_endPage; $i++) {
if ($this->_page == $i)
$this->_result .= '<font color="#ff0000">'.$i.'</font>';
else
$this->_result.= '<a href="'.$this->_url.'page='.$i.'">'.$i.'</a>';
}
if ($this->_page != $this->_pageMax) {
$this->_result .= '<a href="'. $this->_url .'page='.$this->_nextPage.'"><font style="font-family:webdings">4</font></a>';
$this->_result .= '<a href="'.$this->_url.'page='.$this->_pageMax.'"><font style="font-family:webdings">:</font></a>';
} else {
$this->_result.= '<font style="font-family:webdings">4</font><font style="font-family:webdings">:</font>';
}
$this->_result.= '<script type="text/javascript">
function chickForm(){
var submit = true;
var page_num=document.getElementById("page").value;
var exp=/^d*$/;
var objExp=new RegExp(exp);
if(page_num==""){
alert("不能为空");
submit = false;
}else if(!objExp.test(page_num)){
alert("得是数字");
submit = false;
}
return submit;
}
</script>
<form method="get" action="'.$this->_url.'"onsubmit="return chickForm()">
<input type="text" id="page" name="page" style="width:30px; height:25px;" />
<input type="submit" value="go" style=" vertical-align:center;width:30px; height:25px;"/></form>';
return $this->_result;
}
}
?>
2、调用示例