class page{
private $nowPage;//当前页
private $pageSize;//每页记录数
private $navLen;//数字导航条的长度
private $totalRs;//总记录数
public $totalPages;//总页数
public $sql;//SQL语句变量
private $target;//打开链接方式,默认为_self
public $html='';
private $link;//数据库链接标识
/**
*$table 数据表的名称
*$link 数据库链接
*$pageSize 每页记录数,默认为20
*$navLen 数字导航条的长度,默认为7
*/
public function __construct($table,$link,$pageSize=20,$navLen=7,$target=0){
$this->html = '';
$this->pageSize = $pageSize;
$this->navLen = $navLen;
$this->link = $link;
switch($target){
case 0: $this->target = '_self';break;
case 1: $this->target = '_blank';break;
}
//获取总记录数
$this->sql = 'SELECT * FROM '.$table;
$result = @mysql_query($this->sql,$this->link);
$this->totalRs = mysql_num_rows($result);
//获取总页数
$this->totalPages = ceil($this->totalRs/$pageSize);
//获取当前页
$this->nowPage = isset($_GET['p']) ? $_GET['p'] : 1;
//生成导航条代码
$this->html();
//生成分页记录代码
$this->handle();
}
private function handle(){
$this->sql = $this->sql.' LIMIT '.($this->nowPage-1)*$this->pageSize.','.$this->pageSize.'';
}
private function get_first_page(){
if($this->nowPage <= 1){
return '<a href="javascript:void(0)">首页</a>';
}else{
return '<a href="?p=1">首页</a>';
}
}
private function get_prev_page(){
if($this->nowPage <= 1){
return '<a href="javascript:void(0)">上一页</a>';
}else{
return '<a href="?p='.($this->nowPage-1).'">上一页</a>';
}
}
public function get_num_nav(){
$str = '';
$a = intval(($this->nowPage-1)/$this->navLen);
for($i=1;$i<=$this->navLen;$i++){
$c = $a*$this->navLen+$i;
if($c<=$this->totalPages){
if($c==$this->nowPage){
$str .= '<a href="javascript:void(0)" target="'.$this->target.'">'.$c.'</a>';
}else{
$str .= '<a href="?p='.$c.'" target="'.$this->target.'">'.$c.'</a>';
}
}
}
return $str;
}
private function get_next_page(){
if($this->nowPage >= $this->totalPages){
return '<a href="javascript:void(0)">下一页</a>';
}else{
return '<a href="?p='.($this->nowPage+1).'">下一页</a>';
}
}
private function get_last_page(){
if($this->nowPage >= $this->totalPages){
return '<a href="javascript:void(0)">尾页</a>';
}else{
return '<a href="?p='.$this->totalPages.'">尾页</a>';
}
}
private function html(){
$this->html = $this->get_first_page();
$this->html .= $this->get_prev_page();
$this->html .= $this->get_num_nav();
$this->html .= $this->get_next_page();
$this->html .= $this->get_last_page();
}
}
PHP分页类
最新推荐文章于 2021-03-21 08:42:20 发布