• 青云网帮助电脑爱好者在各自领域取得成功!QQ群:77719397
  • 友情链接,合作,联盟
  • 青云在线影视-中国影视门户 高清影视剧、网络视频在线观看
  • 青云5566小游戏,小游戏,在线小游戏,双人小游戏
雨佳商城
    
首页 > 技术专栏 > php >

PHP分页类 class Page

www.qy5566.cn    来源:未知    作者:admin    时间:2009-06-22 21:14    点击:
       核心提示:虽然从网上找的并做一定的修改,但很好用。类 编写 规范 适全性强!里面有详细的介绍! ?php /** 分页类 修改:Silence Creatdate:2006-5-30 LastModify:2009-5-31 */ class Page{ private $mTotalRowsNum =0; //总

虽然从网上找的并做一定的修改,但很好用。类编写规范适全性强!里面有详细的介绍!

 

  1. <?php  
  2. /**  
  3. 分页类  
  4.     修改:Silence  
  5.     Creatdate:2006-5-30  
  6.     LastModify:2009-5-31  
  7. */ 
  8. class Page {  
  9. private $mTotalRowsNum = 0; // 总信息行数  
  10. private $mCurPageNumber = 1; // 当前所在页  
  11. private $mTotalPagesNum = 1; // 总页数  
  12. private $mQueryString// 页面传递的数据(url?后的字符串)  
  13. private $mPageRowsNum = 20; // 每页显示行数  
  14. private $mIndexBarLength = 11; // 索引条的页数  
  15. private $mIndexBar = ''// 页码索引条  
  16. private $mPageInfo = ''// 分页信息  
  17. // 页码索引条样式  
  18. private $mNextButton = "<font style=\"font-family:webdings\">8</font>";  
  19. private $mPreButton = "<font style=\"font-family:webdings\">7</font>";  
  20. private $mFirstButton = "<font style=\"font-family:webdings\">9</font>";  
  21. private $mLastButton = "<font style=\"font-family:webdings\">:</font>";  
  22. private $mCssIndexBarCurPage = "font-weight:bold;color:#FF0000";  
  23. private $mCssIndexBarPage = '';  
  24. // 分页信息样式  
  25. private $mCssPageInfoNumFont = 'color:#FF0000';  
  26. private $mCssPageInfoFont = '';  
  27.  
  28. // 构造方法  
  29. public function __construct(&$rSqlQuery$userPageRowsNum = '') {  
  30.   if (! is_array ( $rSqlQuery )) {  
  31.    $this->SetDbPageBreak ( $rSqlQuery$userPageRowsNum );  
  32.   } else {  
  33.    $this->SetArrayPageBreak ( $rSqlQuery$userPageRowsNum );  
  34.   }  
  35. }  
  36.  
  37. // 设置数据库型分页  
  38. private function SetDbPageBreak(&$rSqlQuery$userPageRowsNum = '') {  
  39.   $this->SetDbTotalRowsNum ( $rSqlQuery );  
  40.   $this->SetTotalPagesNum ( $userPageRowsNum );  
  41.   if ($this->mTotalPagesNum > 1) {  
  42.    $this->SetCurPageNumber ();  
  43.    $this->SetSqlQuery ( $rSqlQuery );  
  44.    $this->SetQueryString ();  
  45.    $this->SetIndexBar ();  
  46.    $this->SetPageInfo ();  
  47.   }  
  48. }  
  49.  
  50. // 设置数组型分页  
  51. private function SetArrayPageBreak(&$rArray$userPageRowsNum = ''$userTotalRowsNum = '') {  
  52.   $this->SetArrayTotalRowsNum ( $rArray$userTotalRowsNum );  
  53.   $this->SetTotalPagesNum ( $userPageRowsNum );  
  54.   if ($this->mTotalPagesNum > 1) {  
  55.    $this->SetCurPageNumber ();  
  56.    $this->SetArray ( $rArray );  
  57.    $this->SetQueryString ();  
  58.    $this->SetIndexBar ();  
  59.    $this->SetPageInfo ();  
  60.   }  
  61. }  
  62.  
  63. // 数据库型计算总行数  
  64. private function SetDbTotalRowsNum($rSqlQuery) {  
  65.   $this->mTotalRowsNum = mysql_num_rows ( mysql_query ( $rSqlQuery ) );  
  66. }  
  67.  
  68. // 数组型计算总行数  
  69. private function SetArrayTotalRowsNum($array) {  
  70.   $this->mTotalRowsNum = count ( $array );  
  71. }  
  72.  
  73. // 计算总页数  
  74. private function SetTotalPagesNum($userPageRowsNum = '') {  
  75.   if ($userPageRowsNum) {  
  76.    $this->mPageRowsNum = $userPageRowsNum;  
  77.   }  
  78.   $this->mTotalPagesNum = ( int ) (floor ( ($this->mTotalRowsNum - 1) / $this->mPageRowsNum ) + 1);  
  79. }  
  80.  
  81. // 计算当前页数  
  82. private function SetCurPageNumber() {  
  83.   if ($_GET ['page']) {  
  84.    $this->mCurPageNumber = $_GET ['page'];  
  85.   }  
  86. }  
  87.  
  88. // 修正Sql截取语句  
  89. private function SetSqlQuery(&$rSqlQuery) {  
  90.   $start_number = ($this->mCurPageNumber - 1) * $this->mPageRowsNum;  
  91.   $rSqlQuery .= " LIMIT " . $start_number . "," . $this->mPageRowsNum;  
  92. }  
  93.  
  94. // 修正截取后的Array  
  95. private function SetArray(&$rArray) {  
  96.   $start_number = ($this->mCurPageNumber - 1) * $this->mPageRowsNum;  
  97.   $rArray = array_slice ( $rArray$start_number$this->mPageRowsNum );  
  98. }  
  99.  
  100. // 修正 $_GET 传递数据  
  101. private function SetQueryString() {  
  102.   $query_string = $_SERVER ['QUERY_STRING'];  
  103.   if ($query_string == '') {  
  104.    $this->mQueryString = "?page=";  
  105.   } else {  
  106.    $this->mQueryString = preg_replace ( "/&?page=\d+/"''$query_string );  
  107.    $this->mQueryString = "?" . $this->mQueryString . "&page=";  
  108.   }  
  109. }  
  110.  
  111. // 设置页码索引条  
  112. private function GetPageIndex() {  
  113.   if ($this->mTotalPagesNum <= $this->mIndexBarLength) {  
  114.    $first_number = 1;  
  115.    $last_number = $this->mTotalPagesNum;  
  116.   } else {  
  117.    $offset = ( int ) floor ( $this->mIndexBarLength / 2 );  
  118.    if (($this->mCurPageNumber - $offset) <= 1) {  
  119.     $first_number = 1;  
  120.    } elseif (($this->mCurPageNumber + $offset) > $this->mTotalPagesNum) {  
  121.     $first_number = $this->mTotalPagesNum - $this->mIndexBarLength + 1;  
  122.    } else {  
  123.     $first_number = $this->mCurPageNumber - $offset;  
  124.    }  
  125.    $last_number = $first_number + $this->mIndexBarLength - 1;  
  126.   }  
  127.   $last_number;  
  128.   for($i = $first_number$i <= $last_number$i ++) {  
  129.    if ($this->mCurPageNumber == $i) {  
  130.     $page_index .= "<font style='" . $this->mCssIndexBarCurPage . "'>" . $i . "</font> ";  
  131.    } else {  
  132.     $page_index .= " <a href='" . $this->mQueryString . $i . "' style='" . $this->mCssIndexBarPage . "'>" . $i . "</a> ";  
  133.    }  
  134.   }  
  135.   return $page_index;  
  136. }  
  137.  
  138. // 设置页码索引条  
  139. private function SetIndexBar() {  
  140.   $this->mIndexBar = $this->GetNavFirstButton ();  
  141.   $this->mIndexBar .= $this->GetNavPreButton ();  
  142.   $this->mIndexBar .= $this->GetPageIndex ();  
  143.   $this->mIndexBar .= $this->GetNavNextButton ();  
  144.   $this->mIndexBar .= $this->GetNavLastButton ();  
  145. }  
  146.  
  147. // 得到页码索引条 首页按钮  
  148. private function GetNavFirstButton() {  
  149.   return "<a href='" . $this->mQueryString . "1'>" . $this->mFirstButton . "</a> ";  
  150. }  
  151.  
  152. // 得到页码索引条 上一页按钮  
  153. private function GetNavPreButton() {  
  154.   if ($this->mCurPageNumber > 1) {  
  155.    $pre_number = $this->mCurPageNumber - 1;  
  156.   } else {  
  157.    $pre_number = 1;  
  158.   }  
  159.   return "<a href='" . $this->mQueryString . $pre_number . "'>" . $this->mPreButton . "</a> ";  
  160. }  
  161.  
  162. // 得到页码索引条 下一页按钮  
  163. private function GetNavNextButton() {  
  164.   if ($this->mCurPageNumber < $this->mTotalPagesNum) {  
  165.    $next_number = $this->mCurPageNumber + 1;  
  166.   } else {  
  167.    $next_number = $this->mTotalPagesNum;  
  168.   }  
  169.   return "<a href='" . $this->mQueryString . $next_number . "'>" . $this->mNextButton . "</a> ";  
  170. }  
  171.  
  172. // 得到页码索引条 末页按钮  
  173. private function GetNavLastButton() {  
  174.   return "<a href='" . $this->mQueryString . $this->mTotalPagesNum . "'>" . $this->mLastButton . "</a> ";  
  175. }  
  176.  
  177. // 设置分页信息  
  178. private function SetPageInfo() {  
  179.   $this->mPageInfo = "<font style='" . $this->mCssPageInfoFont . "'>";  
  180.   $this->mPageInfo .= "共 <font style='" . $this->mCssPageInfoNumFont . "'>" . $this->mTotalRowsNum . "</font> 条信息 | ";  
  181.   $this->mPageInfo .= "<font style='" . $this->mCssPageInfoNumFont . "'>" . $this->mPageRowsNum . "</font> 条/页 | ";  
  182.   $this->mPageInfo .= "共 <font style='" . $this->mCssPageInfoNumFont . "'>" . $this->mTotalPagesNum . "</font> 页 | ";  
  183.   $this->mPageInfo .= "第 <font style='" . $this->mCssPageInfoNumFont . "'>" . $this->mCurPageNumber . "</font> 页";  
  184.   $this->mPageInfo .= "</font>";  
  185. }  
  186.  
  187. // 取出页码索引条  
  188. public function GetIndexBar() {  
  189.   return $this->mIndexBar;  
  190. }  
  191.  
  192. // 取出分页信息  
  193. public function GetPageInfo() {  
  194.   return $this->mPageInfo;  
  195. }  
  196.  
  197. //释放类  
  198. function __destruct() {  
  199.  
  200. }  
  201. }  
  202. ?>  


 

使用方法

 

  1. $page = new page ( $result, 20 ); //$result为返回记录集数组 ,20为返回每页条数  
  2. $index = $page->GetIndexBar () . $page->GetPageInfo ();  
  3. print_r ( $result );  
  4. echo "<br><br>";  
  5. echo "<center>".$index."</center>"

 

  + 相关文章   关键字:,this,private,//,function,