dd

page分页代码,在其它程序和ThinkPHP里面都使用

jerry PHP 2015年11月18日 收藏
分页模块,自己再改改样式即可
<?php

//require SITE_PATH.'/Education/Const/Admin/Model/Paging.const';
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地址

    
    /* 构造方法 */
    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();
    }

    /* 设置每页显示数据的条数 */
    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;
        }
        
        echo $this->m_PagingDataArray['page'];
    }
    
    /* 设置查询数据的起始值 */
    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 <= $this->m_Page) {
            $this->m_Page = $this->m_TotalFraction;
        }
    }
    
    /* 设置分页的url地址 */
    private function SetUrl()
    {
        $this->Setuppase('m_Url', 'url', null);
    }
    
    /* 获取查询数据的起始值 */
    public function GetStarting()
    {
        return $this->m_Starting;
    }
    
    /* 获取每页显示的条数值 */
    public function GetFraction()
    {
        return $this->m_Fraction;
    }
    
    /* 获取url拼接,处理URL拼接方法 */
    public 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.'&';
                }
            }
        }
        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);

        $Html = null;
        
        //当数据只有一页的情况,总页数 等于 1 时
        if($this->m_TotalFraction == 1) {
            $Html .= '上一页       ';
            $Html .= "当前第 $this->m_Page 页</a>       ";
            $Html .= '下一页       ';
            $Html .= '共1页       ';
            $Html .= "共有 $this->m_Total 条数据</a>       ";

            //第一页,(首页的时候,当前页码为 1 时)
            } elseif($this->m_Page == 1) {
            $Html .= '上一页       首页 ';
            $Html .= "当前第 $this->m_Page 页</a>       ";
            $Html .= "<a href=$PageUrly>下一页</a>       ";
            $Html .= "共 $this->m_TotalFraction 页</a>       ";
            $Html .= "共有 $this->m_Total 条数据</a>       ";

            //到尾部的时候,(当前页码 等于 总行数时)
            } elseif($this->m_Page == $this->m_TotalFraction) {
            $Html .= "<a href=$PageUrls>上一页</a>       尾部 ";
            $Html .= "当前第 $this->m_TotalFraction 页</a>       ";
            $Html .= '下一页       ';
            $Html .= "共 $this->m_TotalFraction 页</a>       ";
            $Html .= "共有 $this->m_Total 条数据</a>       ";

            //正常的时候
            } else {
            $Html .= "<a href=$PageUrls>上一页</a>       正常 ";
            $Html .= "当前第 $this->m_Page 页</a>       ";
            $Html .= "<a href=$PageUrly>下一页</a>       ";
            $Html .= "共 $this->m_TotalFraction 页</a>       ";
            $Html .= "共有 $this->m_Total 条数据</a>       ";
        }
    
        return $Html;
    }
    
}

下面来介绍用法:
$Configuration = array(
    'total' => 30,  //总条数
    'traction' => 3,  //当前页面需要显示的条数
    'url' => './StudentManagement',  //分页按钮的url地址
);
//调用分页模块,第一个参数是将页面获取的数据传递进去,$_REQUEST全局变量,POST或者GET的都可以获取大。第二个参数就是上面的数组,配置参数
$PageingObj = new PagingModel($_REQUEST, $Configuration);
$this->assign('pageing', $PageingObj->GetPagingHtmlInfo());  //然后就调用这个方法返回分页代码,放在页面上去就可以了
dd