zhphpframework(二十六) php+mysql 分页类

142 篇文章 0 订阅
25 篇文章 0 订阅
<?php
/**
 * Created by JetBrains PhpStorm.
 * User: 张华
 * Date: 14-3-8
 * Time: 下午12:21
 * QQ: 746502560@qq.com
 * To change this template use File | Settings | File Templates.
 */
final class page {
     private $tableName;#表名
     private $total; #总数量
     private $pageNumber; #每页数量
     private $nowPage;#当前页码数
     private $countPage; #总页码数
     private $limitStart=0; #limit分页开始位置
     private $dbLink=null; #数据库连接资源
     private  $prev;#上一页
     private  $next;#下一页
     private  $pageurl;#当前分页的url
    /**
     * 配置分页连接数组配置文件
     * @var array
     * class div的className
     * id   div的id
     * first 首页
     * last 最后一页
     * next 下一页
     * prev 上一页
     * link_type=true  默认才用翻页 如果为false 就是数字分页
     * custom   自定义样式将依据 link_type 返回需要的所有数据,以数组的方式返回
     */
    public  $config=array( 'class'=>'','id'=>'','first'=>'首页','last'=>'尾页','next'=>'下一页', 'prev'=>'上一页', 'link_type'=>false,'custom'=>false); #分页链接配置文件
    /**
     * 构造函数初始化
     * @param $total
     * @param $pageNumber
     * @param int $p
     */
    public  function  __construct($total,$pageNumber,$p=1){
         $this->total=$total;  #总数据量
         $this->pageNumber=$pageNumber; #每页显示多少条数据
         $nowPage=isset($_GET['p'])?$_GET['p']:$p; #当前页码数
          if(strpos($nowPage,'.html') !== false){
              $nowPage=str_ireplace('.html','',$nowPage);
              $this->nowPage=intval($nowPage);
          }
         $this->nowPage=intval($nowPage);
          $this->getCountPage(); #总页码数字
         $this->limitStart(); #limit Start赋值
         $this->getdbLink(); #数据库连接资源
        $this->geturl();
     }

    /**
     * 得到总页码数
     */
    public  function  getCountPage(){
        $this->countPage=ceil($this->total / $this->pageNumber);
    }

    /**
     * 得到limit分页的开始位置
     */
    private  function  limitStart(){
        $this->limitStart =($this->nowPage-1)*$this->pageNumber;
    }

    /**
     * 得到数据库的链接资源
     * ---如果你应用与其他第三方框架请更改
     */
    private  function  getdbLink($type='pdo'){
        if($type == 'pdo'){
           $this->dbLink=database::init();
        }else{}
   }


    /**
     * 显示分页后的数据
     *
     *  select * from  table  limit start,length where 条件  order by 字段 desc/asc
     * $conditions=array(
     *   'field'=>'id,name,pwd',
     *   'where'=>'id=1 and status=1',
     *    'order'=>'fieldName desc',
     *    'group'=>'feildName'
     * );
     * $conditions=array(
     *   'field'=>array('id','name','pwd')
     *   'where'=>'id=1 and status=1',
     *    'order'=>'fieldName desc',
     *    'group'=>'feildName'
     * );
     * 分页方式
     *    1--数据库limit 分页
     *    2--数组分页
     */
    public  function  dataList($tableName,$conditions=array(),$pageType='2'){
         $conditions=empty($conditions)?array():$conditions;
         $field=isset($conditions['field'])?$conditions['field']:'*';
         if(is_array($field)){ $field=implode(',',$field);}
         $where=isset($conditions['where'])?'WHERE '.$conditions['where']:null;
         $order=isset($conditions['order'])?'ORDER BY '.$conditions['order']:null;
         $group=isset($conditions['group'])?'GROUP BY'.$conditions['group']:null;
         if($pageType == '1'){
            return   $this->dbLimit($field,$tableName,$where,$order,$group);
         }else{
             return $this->arrayLimit($field,$tableName,$where,$order,$group);
         }
    }

    /**
     *  数据库分页
     * @param $field
     * @param $tableName
     * @param $where
     * @param $order
     * @param $group
     * @return array|bool
     */
    private  function  dbLimit($field,$tableName,$where,$order,$group){
        $start=$this->limitStart;
        $field="*";
        $sql='SELECT '.$field.' FROM '.$tableName." ".$where." ".$order.$group.' LIMIT '.$start.','.$this->pageNumber;
        unset($conditions,$field,$where,$order,$group,$result);
        return $this->pageDataAction($sql);
    }

    /**
     *  数组分页
     * @param $field
     * @param $tableName
     * @param $where
     * @param $order
     * @param $group
     */
    private  function  arrayLimit($field,$tableName,$where,$order,$group){
        $sql='SELECT '.$field.' FROM '.$tableName.$where.$order.$group;
        $dataList=array_chunk($this->pageDataAction($sql),$this->pageNumber,true);
        $countDataList=count($dataList);
        $this->nowPage=($this->nowPage <= 1)?0: $this->nowPage-1;
        $this->nowPage=($countDataList <= $this->nowPage)?$countDataList-1:$this->nowPage;
        $data=array_key_exists($this->nowPage,$dataList)?$dataList[$this->nowPage]:null;
        unset($conditions,$field,$where,$order,$group,$result,$sql);
        return $data;
     }

    /**
     *  数据处理核心部分
     * @param $sql
     * @return array|bool
     */
    private  function  pageDataAction($sql,$type='pdo'){
        $data=array();
         if($type == 'pdo'){
             $data= $this->dbLink->queryAll($sql);
             unset($sql);
             return $data;
         }elseif($type == 'mysql'){
             $result=mysql_query($sql,$this->dbLink);
             if(is_resource($result)){
                 $data=array();
                 while($rows=mysql_fetch_assoc($result)){
                     $data[]=$rows;
                 }
                 mysql_free_result($result);
             }else{
                 return false;
             }
             unset($sql,$result);
             return $data;
         }
    }

    private  function geturl(){
        $arr=array();
       if(!empty($_SERVER["REQUEST_URI"])){
            $nowurl = $_SERVER["REQUEST_URI"];
            if(strpos($nowurl,'?') !== false){
                $search=strrchr($nowurl,'?');
               $this->pageurl=str_ireplace($search,'',$nowurl);
            }else{
                $arr=explode('/',$nowurl);
                 $end=end($arr);
               if(empty($end)){
                   $this->pageurl=$nowurl;
               }else{
                   if(is_numeric($end) || strpos($end,'.html')){
                      $this->pageurl=str_ireplace($end,'',$nowurl);
                   }
               }
            }
        }else{
           $this->pageurl=$_SERVER["PHP_SELF"];//当前文件
        }

    }


    /**
     * 显示分页字符串
     */
    public  function  show($config=array()){
      $config=empty($config)?$this->config:$config;
       $prev=($this->nowPage < 1)? 1:$this->nowPage;
        $nowpage=intval($this->nowPage)+1;
       $next=($nowpage > $this->countPage)?$this->countPage:$nowpage+1;
        if($config['custom'] == true){
             return array('nowpage'=>$nowpage,'pre'=>$prev,'next'=>$next,'countPage'=>$this->countPage,'url'=>$this->pageurl);
        }
        $link='<div id="'.$config['id'].'" class="'.$config['class'].'" style="width:auto; margin:10px auto; padding:6px 0px; height:20px; border: 0px solid #333;"><ul style="list-style:none;margin:0 auto; padding: 0;">';
        $link.='<li style="float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;">
        <a href="'.$this->pageurl.'?p=1.html" style="color:#333; text-decoration:none;">'.$config['first'].'</a></li>';
        $link.='<li style="float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;">
        <a href="'.$this->pageurl.'?p='.$prev.'.html"  style="color:#333; text-decoration:none;">'.$config['prev'].'</a></li>';
            if($config['link_type'] == true){
                     for($i=1;$i<=$this->countPage;$i++){
                         $link.='<li style="float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;">
                         <a href="'.$this->pageurl.'?p='.$i.'"  style="color:#333; text-decoration:none;display: inline-block; width: 20px;text-align: center;">'.$i.'</a></li>';
                         }
             }
    $link.='<li style="float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;">
    <a href="'.$this->pageurl.'?p='.$next.'.html" style="color:#333; text-decoration:none;">'.$config['next'].'</a></li>';
    $link.='<li style="float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;">
    <a href="'.$this->pageurl.'?p='.$this->countPage.'.html" style="color:#333; text-decoration:none;">'.$config['last'].'</a></li>';
    $link.='<li style="color:#555; text-decoration:none;float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;">当前第'.(intval($this->nowPage) + 1).'页</li>';
    $link.='<li style="color:#555; text-decoration:none;float:left; border:1px solid #5d9cdf; height:20px; line-height:20px; margin:0px 2px;">共'.$this->countPage.'页</li>';
    $link.='</ul></div>';
    return $link;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值