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

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

  2. //require SITE_PATH.'/Education/Const/Admin/Model/Paging.const';
  3. class PagingModel {
  4.     
  5.     private $m_PagingDataArray;  //接收页面提交的post或者get的一维数组条件
  6.     private $m_Configuration;  //配置项数据
  7.     private $m_Fraction;  //每个页面显示的条数
  8.     private $m_Total;  //数据的总条数
  9.     private $m_Page;  //页面传递过来的页码值
  10.     private $m_Starting;  //查询数据的起始值
  11.     private $m_TotalFraction;  //计算出来的总页数
  12.     private $m_Url;  //分页使用的url地址

  13.     
  14.     /* 构造方法 */
  15.     public function __construct($PagingDataArray = array(), $Configuration = array())
  16.     {
  17.         /* 初始化属性数据 */
  18.         $this->m_PagingDataArray = array();
  19.         $this->m_Configuration = array();
  20.         
  21.         /* 基础数据设置 */
  22.         $this->SetPagingDataArray($PagingDataArray);
  23.         $this->SetConfiguration($Configuration );
  24.         $this->SetBasisData();
  25.     }
  26.     
  27.     /* 设置数据 */
  28.     private function SetPagingDataArray($PagingDataArray)
  29.     {
  30.         /* 判断配置项的数据是否为空 */
  31.         if(false == empty($PagingDataArray)) {
  32.             $this->m_PagingDataArray = $PagingDataArray;
  33.         } else {
  34.             $this->m_PagingDataArray = array();
  35.         }
  36.     }
  37.     
  38.     /* 设置配置项数据 */
  39.     private function SetConfiguration($Configuration)
  40.     {
  41.         /* 判断配置项的数据是否为空 */
  42.         if(false == empty($Configuration)) {
  43.             $this->m_Configuration = $Configuration;
  44.         } else {
  45.             $this->m_Configuration = array();
  46.         }
  47.     }
  48.     
  49.     
  50.     /* 处理判断数组中是否存在某个键名 */
  51.     private function Setuppase($Property, $Key, $Content)
  52.     {
  53.         /* 判断 $Key 是否在数组中存在的键名 */
  54.         if(true == array_key_exists($Key, $this->m_Configuration)) {
  55.             $this->$Property = $this->m_Configuration["$Key"];
  56.         } else {
  57.             $this->$Property = $Content;
  58.         }
  59.     }
  60.     
  61.     /* 基础数据设置 */
  62.     private function SetBasisData()
  63.     {
  64.         $this->SetFraction();
  65.         $this->SetTotal();
  66.         $this->SetPage();
  67.         $this->SetStarting();
  68.         $this->SetTotalFraction();
  69.         $this->SetUrl();
  70.     }

  71.     /* 设置每页显示数据的条数 */
  72.     private function SetFraction()
  73.     {
  74.         $this->Setuppase('m_Fraction', 'traction', 15);
  75.     }
  76.     
  77.     /* 设置数据的总条数 */
  78.     private function SetTotal()
  79.     {
  80.         $this->Setuppase('m_Total', 'total', 0);
  81.     }
  82.     
  83.     /* 设置页面传递过来的页码值 */
  84.     private function SetPage()
  85.     {
  86.         /* 判断 $Key 是否在数组中存在的键名 */
  87.         if(true == array_key_exists('page', $this->m_PagingDataArray)) {
  88.             $this->m_Page = max(1, (false == empty($this->m_PagingDataArray['page']) ? intval($this->m_PagingDataArray['page']) : 0));
  89.         } else {
  90.             $this->m_Page = 1;
  91.         }
  92.         
  93.         echo $this->m_PagingDataArray['page'];
  94.     }
  95.     
  96.     /* 设置查询数据的起始值 */
  97.     private function SetStarting()
  98.     {
  99.         $this->m_Starting = ($this->m_Page - 1) * $this->m_Fraction;
  100.     }
  101.     
  102.     /* 设置计算出来的总页数, 总页数 = 总条数除以每页显示的条数。*/
  103.     private function SetTotalFraction()
  104.     {
  105.         $this->m_TotalFraction = ceil($this->m_Total/$this->m_Fraction);
  106.         
  107.         /* 当前页数大于最大页数时,将总页数的值赋值给当前页面,防止超越操作。*/
  108.         if($this->m_TotalFraction <= $this->m_Page) {
  109.             $this->m_Page = $this->m_TotalFraction;
  110.         }
  111.     }
  112.     
  113.     /* 设置分页的url地址 */
  114.     private function SetUrl()
  115.     {
  116.         $this->Setuppase('m_Url', 'url', null);
  117.     }
  118.     
  119.     /* 获取查询数据的起始值 */
  120.     public function GetStarting()
  121.     {
  122.         return $this->m_Starting;
  123.     }
  124.     
  125.     /* 获取每页显示的条数值 */
  126.     public function GetFraction()
  127.     {
  128.         return $this->m_Fraction;
  129.     }
  130.     
  131.     /* 获取url拼接,处理URL拼接方法 */
  132.     public function GetUrlSplice()
  133.     {
  134.         $UrlSplice = '?';
  135.         if(false == empty($this->m_PagingDataArray)) {
  136.             //删除当前数组中的page数据
  137.             unset($this->m_PagingDataArray['page']);
  138.             foreach($this->m_PagingDataArray as $PKey=>$pValue) {
  139.                 /* 拼接普通url */
  140.                 if((false == empty($pValue)) && (false == is_array($pValue))) {
  141.                     $UrlSplice .= $PKey.'='.$pValue.'&';
  142.                 }
  143.             }
  144.         }
  145.         return $UrlSplice;
  146.     }
  147.     
  148.     
  149.     /* 返回拼接好的html代码(包括js代码) */
  150.     public function GetPagingHtmlInfo()
  151.     {
  152.         $UrlSplice = $this->GetUrlSplice();
  153.         
  154.         $PageUrl = $this->m_Url.$UrlSplice.'page=';
  155.         $PageUrls = $PageUrl.($this->m_Page-1);
  156.         $PageUrly = $PageUrl.($this->m_Page+1);

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

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

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

  180.             //正常的时候
  181.             } else {
  182.             $Html .= "<a href=$PageUrls>上一页</a>       正常 ";
  183.             $Html .= "当前第 $this->m_Page 页</a>       ";
  184.             $Html .= "<a href=$PageUrly>下一页</a>       ";
  185.             $Html .= "共 $this->m_TotalFraction 页</a>       ";
  186.             $Html .= "共有 $this->m_Total 条数据</a>       ";
  187.         }
  188.     
  189.         return $Html;
  190.     }
  191.     
  192. }

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