<?php
/*
* 分页模块
*/
interface Page{
public function showpage();
}
class AdminPage implements Page{
/*
* 构造参数
* SQL语句,每页显示数,url
*/
private $sql;
private $pageline; //每页显示多少行数据
private $urlstr;
private $nowpage;
private $totalNum;
private $pageNum;
private $pageStr;// 形成分页字符串
public function __construct($sql,$p){
$this->sql = $sql;
$this->pageline = $p;
$this->urlstr = $_SERVER['SCRIPT_NAME'];
if(empty($_GET['p'])){
$this->nowpage = 1; //初始化当前页数为1
}else if(is_numeric($_GET['p'])){
$this->nowpage = $_GET['p'];
}else{
$e = new Error(6);
$e->show();
}
$s = new Sql();
$this->totalNum = $s->results_exist_num($this->sql);
$this->pageNum = ceil($this->totalNum/$this->pageline);
}
/*
* 形式为:
* 首页| 上一页| 1 | 2 | 3 | 4 | 5 |下一页 | 末页
*
*
*/
public function showpage(){
if($this->pageNum >=1 && $this->nowpage <5 ){
if( $this->pageNum <=5){
$this->pageStr = '<ul id="pages"><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
if($this->nowpage!=1){
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
}
for($i=1;$i<=$this->pageNum;$i++){
if($i==$this->nowpage){
$this->pageStr .='<li id="spages"><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}else{
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}
}
if($this->nowpage !=$this->pageNum){
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
}
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
echo $this->pageStr;
}else if($this->pageNum>5){
$this->pageStr = '<ul id="pages"><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
if($this->nowpage !=1){
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
}
for($i=1;$i<=5;$i++){
if($i==$this->nowpage){
$this->pageStr .='<li id="spages"><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}else{
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}
}
if($this->nowpage !=$this->pageNum){
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
}
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
echo $this->pageStr;
}
}else if($this->nowpage <= $this->pageNum -2 && $this->nowpage>=5){
$this->pageStr = '<ul id="pages"><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
for($i=$this->nowpage-2;$i<=$this->nowpage+2;$i++){
if($i==$this->nowpage){
$this->pageStr .='<li id="spages"><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}else{
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}
}
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
echo $this->pageStr;
}else if($this->nowpage>=$this->pageNum-3 && $this->nowpage <=$this->pageNum){
$this->pageStr = '<ul id="pages"><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
for($i=$this->pageNum-4;$i<=$this->pageNum;$i++){
if($i==$this->nowpage){
$this->pageStr .='<li id="spages"><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}else{
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
}
}
if($this->nowpage !=$this->pageNum){
$this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
}
$this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
echo $this->pageStr;
}
}
}
|