Feb16

分享我的PHP分页函数类[原创]

Author: leeon  Click: 5227   Date: 2010.02.16 @ 21:05:45 pm Category: php
构造函数中的两个参数为查询的SQL语句和每页显示的行数
最终样式为:

CSS源码:

#pages{display:block; text-align:center; overflow:hidden; color:#000; font-size:13px}
#pages a{color:#333; text-decoration:none; font-family:Verdana,Geneva,sans-serif; padding:2px 3px; display:block}
#pages li{float:left; display:inline-block; border:1px solid #999; margin-right:3px; margin-left:3px}
#pages #spages{background:#CCC; font-weight:bold; color:#36C}

PHP分页函数类源码:

<?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;

}
}
}


TAG:   php 分页

    评论
    • 提交

    分类

    标签

    归档

    最新评论

    shallwe99在10:21:17评论了
    【原创】如何在微信小程序开发中正确的使用vant ui组件
    默一在09:04:53评论了
    Berkeley DB 由浅入深【转自架构师杨建】
    Memory在14:09:22评论了
    【原创】最佳PHP框架选择(phalcon,yaf,laravel,thinkphp,yii)
    leo在17:57:04评论了
    shell中使用while循环ssh的注意事项
    呵呵哒啦在15:06:33评论了
    【原创】最佳PHP框架选择(phalcon,yaf,laravel,thinkphp,yii)

    我看过的书

    链接

    其他

    访问本站种子 本站平均热度:6767 c° 本站链接数:29 个 本站标签数:458 个 本站被评论次数:93 次