封装好的分页类

提示:仅供参考

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


阅读更多
版权声明:本文为郝云原创文章,未经郝云允许不得转载。 https://blog.csdn.net/haoyunyun888/article/details/51544305
个人分类: php smarty类
想对作者说点什么? 我来说一句

封装的一个完整的分页类

qq_34284638 qq_34284638

2016-09-06 20:16:17

阅读数:665

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭