//1.首先定义一个分页类
<?php
class page{
//总记录条数
private $total;
//每页显示记录条数
private $nums;
//共有多少页
private $pages;
//当前是那一页
private $cpage;
//链接URL
private $uri;
public function __construct($total,$nums){
$this->total=$total;
$this->nums=$nums;
$this->pages=$this->getpage();
$this->cpage=($_GET['page'])?$_GET['page']:1;
$this->uri="test.php";
}
private function getpage(){
return ceil($this->total/$this->nums);
}
private function first(){
if($this->cpage>1){
$prev=$this->cpage-1;
return '<a href="'.$this->uri.'?page=1">首页</a> <a href="'.$this->uri.'?page='.$prev.'">上一页</a>';
}else{
return "";
}
}
private function flist(){
$nums=4;
$list='';
//显示当前页之前的页数
for($i=$nums;$i>=1;$i--){
$page=$this->cpage-$i;
if($page>=1){
$list.=' <a href="'.$this->uri.'?page='.$page.'">'.$page.'</a> ';
}
}
//显示当前页
$list.=" ".$this->cpage." ";
//显示当前页之后的页数
for($i=1;$i<=$nums;$i++){
$page=$this->cpage+$i;
if($page<=$this->pages){
$list.=' <a href="'.$this->uri.'?page='.$page.'">'.$page.'</a> ';
}else{
break;
}
}
return $list;
}
private function last(){
if($this->cpage<$this->pages){
$next=$this->cpage+1;
return '<a href="'.$this->uri.'?page='.$next.'">下一页</a> <a href="'.$this->uri.'?page='.$this->pages.'">末页</a>';
}else{
return "";
}
}
private function start(){
return ($this->cpage-1)*$this->nums+1;
}
private function end(){
return min($this->cpage*$this->nums,$this->total);
}
private function currnum(){
/*
if($this->cpage<$this->pages){
return $this->nums;
}else{
return $this->total-($this->pages-1)*$this->nums;
}
*/
//或者用另外一种方法来计算最后一页显示记录条数
return $this->end()-$this->start()+1;
}
function view(){
return "总共有{$this->total}条记录 本页显示{$this->currnum()}条记录 从{$this->start()}-{$this->end()}条 {$this->cpage}/{$this->pages}
{$this->first()} {$this->flist()} {$this->last()} ";
}
}
?>
//2.包含这个分页类并输出
<?php
include "page.class.php";
$pages=new page(99,10);
echo $pages->view();
?>