封装好的分页类

提示:仅供参考

page.class.php

<meta charset="utf8">
<style>
	a {
		display:inline-block;
		border:1px solid #ccc;
		text-decoration:none;
		padding:5px;
		margin:20px 3px;
		color:#333;
	}
	.apage{
		background:#ccc;
	}
	.disable{
		border:1px solid #ccc;
		color:#ccc;
	}
</style>

<?php
require "mysql.class.php";
class page {
	private $total;
	private $number=10;
	private $pagecount;
	public $limit;
	private $url;
	private $page;
    function __construct($total,$number,$url) {
    	$this->total=$total;
    	$this->number=$number;
    	$this->pagecount=ceil($this->total/$this->number);
    	$this->getpage();
    	$this->url=$url;
		$this->limit="limit ".$this->setlimit();
    }
    private function getpage(){
    	$this->page=(isset($_GET['page'])&&!empty($_GET['page']))?$_GET['page']:1;
		if(preg_match('/\D/',$this->page)){
			$this->page=1;
		}
		if($this->page<=0){
			$this->page=1;
		}
		if($this->page > $this->pagecount){
			$this->page=$this->pagecount;
		}
    }
    private function setlimit(){
    	$start=($this->page-1)*$this->number;
    	return ($start.','.$this->number);
    }

    private function setnumber($num){
    	$str="";
		if($this->pagecount<=$num){
			for($i=1;$i<=$this->pagecount;$i++){
				if($this->page==$i){
					$str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";
				}else{
					$str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";
					//echo "<a href='demo1.php?page=".$i ."&number=".$number."'>".$i."</a>";
				}
			}
		}else{
			if($this->page<=ceil($num/2)){
				for($i=1;$i<=$num;$i++){
					//给当前页加一个灰色的背景
					if($this->page==$i){
						$str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";
					}else{
						$str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";
					}
				}
			}elseif($this->page>=$this->pagecount-floor($num/2)){
				//当前页在后3页时,能实现的效果是,点击后三页的时候,分页不会动
				for($i=$this->pagecount-$num-1;$i<=$this->pagecount;$i++){
					//给当前页加一个灰色的背景
					if($this->page==$i){
						$str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";
					}else{
						$str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";
					}
				}
			}else{
				//将当前页放在中间页数时
				for($i=$this->page-floor($num/2);$i<=$this->page+floor($num/2);$i++){
					if($this->page==$i){
						$str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";
					}else{
						$str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";
					}
				}
			}
		}
		return $str;
    }
    public function fpage($number=false,$num=5){
        $str="";
        if($this->page==1){
            $str.="<a href='javascript:void(0);' class='disable'>首页</a>";
            $str.="<a href='javascript:void(0);' class='disable'>上一页</a>";
        }else{
            $str.="<a href='".$this->url."?page=1'>首页</a>";
            $str.="<a href='".$this->url."?page=".($this->page-1)."'>上一页</a>";
        }
        if($number){
            $str.=$this->setnumber($num);
        }
        if($this->page==$this->pagecount){
            $str.="<a href='javascript:void(0);' class='disable'>下一页</a>";
            $str.="<a href='javascript:void(0);' class='disable'>尾页</a>";
        }else{
            $str.="<a href='".$this->url."?page=".($this->page+1)."'>下一页</a>";
            $str.="<a href='".$this->url."?page=".$this->pagecount."'>尾页</a>";
        }
        return $str;
    }
}

2.显示出来

<?php
include "libs/Smarty.class.php";
include "page.class.php";
$smarty=new Smarty();
//include "mysql.class.php";
//连接数据库
$mysql=new Mysql('127.0.0.1','root','root','1408phpe');
//查询总条数
$total=$mysql->counts("select * from exam8 ");
//$sql="select * from exam8 ";
$page=new page($total,2,'show1.php');
$sql="select * from exam8 $page->limit";
$a=$mysql->select_all($sql);
$smarty->assign('arr',$a);
$smarty->display('show1.tpl');
echo"<p align='left'>".$page->fpage(true)."</p>";


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值