首页 > php教程 > php手册 > 正文

完善的page分页模块,和百度一模一样

php中文网
发布: 2016-06-07 11:42:56
原创
1367人浏览过

http://www.schoolcms.cn/ 发布
完善的page分页模块,和百度一模一样
完善的page分页模块,和百度一模一样
class PagingModel
{
    private $m_PagingDataArray;  //接收页面提交的post或者get的一维数组条件
    private $m_Configuration;  //配置项数据
    private $m_Fraction;  //每个页面显示的条数
    private $m_Total;  //数据的总条数
    private $m_Page;  //页面传递过来的页码值
    private $m_Starting;  //查询数据的起始值
    private $m_TotalFraction;  //计算出来的总页数
    private $m_Url;  //分页使用的url地址
    private $m_PageCoent;  //是否开启页面数字分页按钮

    
    /*
        构造方法
    */
    public function __construct($PagingDataArray = array(), $Configuration = array())
    {
        /* 初始化属性数据 */
        $this->m_PagingDataArray = array();
        $this->m_Configuration = array();
        
        /* 基础数据设置 */
        $this->SetPagingDataArray($PagingDataArray);
        $this->SetConfiguration($Configuration );
        $this->SetBasisData();
    }
    
    /*
        设置数据
    */
    private function SetPagingDataArray($PagingDataArray)
    {
        /* 判断配置项的数据是否为空 */
        if(false == empty($PagingDataArray)) {
            $this->m_PagingDataArray = $PagingDataArray;
        } else {
            $this->m_PagingDataArray = array();
        }
    }
    
    /*
        设置配置项数据
    */
    private function SetConfiguration($Configuration)
    {
        /* 判断配置项的数据是否为空 */
        if(false == empty($Configuration)) {
            $this->m_Configuration = $Configuration;
        } else {
            $this->m_Configuration = array();
        }
    }
    
    
    /*
        处理判断数组中是否存在某个键名
    */
    private function Setuppase($Property, $Key, $Content)
    {
        /* 判断 $Key 是否在数组中存在的键名 */
        if(true == array_key_exists($Key, $this->m_Configuration)) {
            $this->$Property = $this->m_Configuration["$Key"];
        } else {
            $this->$Property = $Content;
        }
    }
    
    /*
        基础数据设置
    */
    private function SetBasisData()
    {
        $this->SetFraction();
        $this->SetTotal();
        $this->SetPage();
        $this->SetStarting();
        $this->SetTotalFraction();
        $this->SetUrl();
        $this->SetPageCoent();
    }

    /*
        设置每页显示数据的条数
    */
    private function SetFraction()
    {
        $this->Setuppase('m_Fraction', 'traction', 15);
    }
    
    /*
        设置数据的总条数
    */
    private function SetTotal()
    {
        $this->Setuppase('m_Total', 'total', 0);
    }
    
    /*
        设置页面传递过来的页码值
    */
    private function SetPage()
    {
        /* 判断 $Key 是否在数组中存在的键名 */
        if(true == array_key_exists('page', $this->m_PagingDataArray)) {
            $this->m_Page = max(1, (false == empty($this->m_PagingDataArray['page']) ? intval($this->m_PagingDataArray['page']) : 0));
        } else {
            $this->m_Page = 1;
        }
    }
    
    /*
        设置查询数据的起始值
    */
    private function SetStarting()
    {
        $this->m_Starting = ($this->m_Page - 1) * $this->m_Fraction;
    }
    
    /*
        设置计算出来的总页数, 总页数 = 总条数除以每页显示的条数。
    */
    private function SetTotalFraction()
    {
        $this->m_TotalFraction = ceil($this->m_Total/$this->m_Fraction);
        
        /* 当前页数大于最大页数时,将总页数的值赋值给当前页面,防止超越操作。*/
        if($this->m_TotalFraction m_Page) {
            $this->m_Page = $this->m_TotalFraction;
        }
    }
    
    /*
        设置分页的url地址
    */
    private function SetUrl()
    {
        $this->Setuppase('m_Url', 'url', null);
    }
    
    /*
        设置是否开启显示数字分页按钮
    */
    private function SetPageCoent()
    {
        $this->Setuppase('m_PageCoent', 'pagecoent', 0);
    }
    
    /*
    获取查询数据的起始值
    */
    public function GetStarting()
    {
        return $this->m_Starting;
    }
    
    /*
        获取每页显示的条数值
    */
    public function GetFraction()
    {
        return $this->m_Fraction;
    }
    
    /*
        获取拼接的每页显示的数字页码
    */
    private function GetPageCoent($PageUrl)
    {
        /* 如果page值不等于1的时候 */
        if($this->m_Page != 1) {
            /* 如果分页数值加显示的分页个数值大于当前总页码数的时候 */
            if(($this->m_Page+$this->m_PageCoent) > $this->m_TotalFraction) {
                /* 计算起始值 */
                $Pageis = $this->m_Page-$this->m_PageCoent;
                /* 计算最大数值 */
                $PageMax = $this->m_TotalFraction;
                
            /* 如果分页数值加显示的分页个数值不大于当前总页码数的时候 */
            } else {
                /* 计算起始值,如果当前page小于等于显示的页数时,就将起始设置为1,防止负数 */
                if($this->m_Page m_PageCoent) {
                    $Pageis = 1;
                } else {
                    $Pageis = $this->m_Page-$this->m_PageCoent;
                }
                
                
                /* 计算最大数值,当前page数值加需要显示的页码个数值 */
                $PageMax = (($this->m_Page+$this->m_PageCoent));
            }
            
            /* 如果显示页码值大于等于总页码值时,将起始值设置为1 */
            if($this->m_PageCoent >= $this->m_TotalFraction) {
                $Pageis = 1;
            }
            
        /* 如果page等于1的时候 */
        } else {
            /* 如果显示页码值大于等于总页码值时,就将总页码值赋值给循环的最大值 */
            if($this->m_PageCoent >= $this->m_TotalFraction) {
                $PageMax = $this->m_TotalFraction;
            } else {
                $PageMax = $this->m_PageCoent+1;
            }
            $Pageis = 1;
        }
        
        /* 循环拼接需要显示的分页数值个数代码 */
        $PageCoent = '

  • ';
            for($Pagei=$Pageis; $Pagei             if($this->m_Page == $Pagei) {
                    $PageCoent .= ''.$Pagei.'';
                } else {
                    $PageCoent .= ''.$Pagei.'';
                }
                
            }
            /* 返回拼接好的代码 */
            return $PageCoent;
        }
        
        
        /*
            获取url拼接,处理URL拼接方法
        */
        private function GetUrlSplice()
        {
            $UrlSplice = '?';
            if(false == empty($this->m_PagingDataArray)) {
                //删除当前数组中的page数据
                unset($this->m_PagingDataArray['page']);
                foreach($this->m_PagingDataArray as $PKey=>$pValue) {
                    /* 拼接普通url */
                    if((false == empty($pValue)) && (false == is_array($pValue))) {
                        $UrlSplice .= $PKey.'='.$pValue.'&';
                    }
                    
                    /* 拼接是数组的url */
                    /*if((false == empty($pValue)) && (true == is_array($pValue))) {
                        
                    }*/
                }
                //print_r($this->m_PagingDataArray);
            }
            return $UrlSplice;
        }
        
        
        /*
            返回拼接好的html代码(包括js代码)
        */
        public function GetPagingHtmlInfo()
        {
            $UrlSplice = $this->GetUrlSplice();
            
            $PageUrl = $this->m_Url.$UrlSplice.'page=';
            $PageUrls = $PageUrl.($this->m_Page-1);
            $PageUrly = $PageUrl.($this->m_Page+1);
            
            if($this->m_PageCoent > 0) {
                $PageCoent = $this->GetPageCoent($PageUrl);
            } else {
                $PageCoent = null;
            }
            

            /* 定义分页数据 */
            $Html = '
      ';
              
              $Home = '
    • 首页
    • ';
              $Previous = '
    • 上一页
    • ';
              $Next = '
    • 下一页
    • ';
              $End = '
    • m_TotalFraction.'">尾页
    • ';
              
              $HomeS = '
    • 首页
    • ';
              $PreviousS = '
    • 上一页
    • ';
              $NextS = '
    • 下一页
    • ';
              $EndS = '
    • 尾页
    • ';
              
              
              /* 当只有一页数据的时候,就没有拼接url地址 */
              if($this->m_TotalFraction == 1) {
                  $Html .= $HomeS.$PreviousS.$PageCoent.$NextS.$EndS;
              /* 当没有数据的时候,就没有拼接url地址 */
              } elseif($this->m_Page == $this->m_TotalFraction && $this->m_Total == 0) {
                  $Html .= $HomeS.$PreviousS.$PageCoent.$NextS.$EndS;
              /* 当为第一页的时候 */
              } elseif($this->m_Page == 1) {
                  $Html .= $HomeS.$PreviousS.$PageCoent.$Next.$End;

              /* 到尾部的时候 */
              } elseif($this->m_Page == $this->m_TotalFraction  && $this->m_TotalFraction > 1) {
                  $Html .= $Home.$Previous.$PageCoent.$NextS.$EndS;

              /* 正常的时候 */
              } else {
                      $Html .= $Home.$Previous.$PageCoent.$Next.$End;
              }
              $Html .= '
    • 当前第'.$this->m_Page.'
    • '.$this->m_TotalFraction.'
    • 总有'.$this->m_Total.'条数据
    ';

            /* css代码 */
            $Css = '';
                
            return $Html.$Css;
        }
        
    }
    //使用方法
    /* 调用分页模块 */
            $Configuration = array(
                'total' => $StudentCount,  //数据总条数
                'traction' => $StudentPage,  //每页显示条数
                'pagecoent' => 3,  //分页显示的个数
                'url' => './StudentManagement',  //翻页的url地址
            );
                   //$_REQUEST : 如果当前查询有其它条件将会自动选择拼接起来
                  //$Configuration : 配置项
            $PageingObj = new PagingModel($_REQUEST, $Configuration);
            $this->assign('pageing', $PageingObj->GetPagingHtmlInfo());

    AD:真正免费,域名+虚机+企业邮箱=0元

  • 最佳 Windows 性能的顶级免费优化软件
    最佳 Windows 性能的顶级免费优化软件

    每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

    下载
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习
    PHP中文网抖音号
    发现有趣的

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号