怕水平的不高,所以从来没有放过任何代码,这个类我已经用了很久,近来用面向对象方法重写,适用于PHP5.x,特地扔出,不怕见笑,希望抛砖引玉。
这个类适用于配合数据库查询分页,和数组分页,下面有使用方法。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class PageBreak {
-
- private $mTotalRowsNum = 0;
-
- private $mCurPageNumber = 1;
-
- private $mTotalPagesNum = 1;
-
- private $mQueryString;
-
- private $mPageRowsNum = 20;
-
- private $mIndexBarLength = 5;
-
- private $mIndexBar = '';
-
- private $mPageInfo = '';
-
-
-
- private $mNextButton = "8";
-
- private $mPreButton = "7";
-
- private $mFirstButton = "9";
-
- private $mLastButton = ":";
-
- private $mCssIndexBarCurPage = "fontweight:bold;color:#FF0000";
-
- private $mCssIndexBarPage = '';
-
-
-
- private $mCssPageInfoNumFont = 'color:#FF0000';
-
- private $mCssPageInfoFont = '';
-
-
-
- public function __construct(&$rSqlQuery, $userPageRowsNum='') {
-
- if( !is_array($rSqlQuery) ) {
-
- $this>SetDbPageBreak($rSqlQuery, $userPageRowsNum);
-
- }
-
- else {
-
- $this>SetArrayPageBreak($rSqlQuery, $userPageRowsNum);
-
- }
-
- }
-
-
-
- private function SetDbPageBreak(&$rSqlQuery, $userPageRowsNum='') {
-
- $this>SetDbTotalRowsNum($rSqlQuery);
-
- $this>SetTotalPagesNum($userPageRowsNum);
-
- if( $this>mTotalPagesNum > 1 ) {
-
- $this>SetCurPageNumber();
-
- $this>SetSqlQuery($rSqlQuery);
-
- $this>SetQueryString();
-
- $this>SetIndexBar();
-
- $this>SetPageInfo();
-
- }
-
- }
-
-
-
- private function SetArrayPageBreak(&$rArray, $userPageRowsNum='', $userTotalRowsNum='') {
-
- $this>SetArrayTotalRowsNum($rArray, $userTotalRowsNum);
-
- $this>SetTotalPagesNum($userPageRowsNum);
-
- if( $this>mTotalPagesNum > 1 ) {
-
- $this>SetCurPageNumber();
-
- $this>SetArray($rArray);
-
- $this>SetQueryString();
-
- $this>SetIndexBar();
-
- $this>SetPageInfo();
-
- }
-
- }
-
-
-
- private function SetDbTotalRowsNum($rSqlQuery) {
-
- $this>mTotalRowsNum = mysql_num_rows( mysql_query($rSqlQuery) );
-
- }
-
-
-
- private function SetArrayTotalRowsNum($array) {
-
- $this>mTotalRowsNum = count($array);
-
- }
-
-
-
- private function SetTotalPagesNum($userPageRowsNum='') {
-
- if( $userPageRowsNum ) {
-
- $this>mPageRowsNum = $userPageRowsNum;
-
- }
-
- $this>mTotalPagesNum = (int)( floor( ($this>mTotalRowsNum1)/$this>mPageRowsNum )+1 );
-
- }
-
-
-
- private function SetCurPageNumber() {
-
- if( $_GET['cur_page'] ) {
-
- $this>mCurPageNumber = $_GET['cur_page'];
-
- }
-
- }
-
-
-
- private function SetSqlQuery(&$rSqlQuery) {
-
- $start_number = ($this>mCurPageNumber1)*$this>mPageRowsNum;
-
- $rSqlQuery .= " LIMIT ".$start_number.",".$this>mPageRowsNum;
-
- }
-
-
-
- private function SetArray(&$rArray) {
-
- $start_number = ($this>mCurPageNumber1)*$this>mPageRowsNum;
-
- $rArray = array_slice($rArray, $start_number, $this>mPageRowsNum);
-
- }
-
-
-
- private function SetQueryString() {
-
- $query_string = $_SERVER['QUERY_STRING'];
-
- if ( $query_string == '' ) {
-
- $this>mQueryString = "?cur_page=";
-
- }
-
- else {
-
- $this>mQueryString = preg_replace("/&?cur_page=d+/", '', $query_string);
-
- $this>mQueryString = "?".$this>mQueryString."&cur_page=";
-
- }
-
- }
-
-
-
- private function GetPageIndex() {
-
- if( $this>mTotalPagesNum <= $this>mIndexBarLength ) {
-
- $first_number = 1;
-
- $last_number = $this>mTotalPagesNum;
-
- }
-
- else {
-
- $offset = (int)floor($this>mIndexBarLength/2);
-
- if( ($this>mCurPageNumber$offset) <= 1 ) {
-
- $first_number = 1;
-
- }
-
- elseif( ($this>mCurPageNumber+$offset) > $this>mTotalPagesNum ) {
-
- $first_number = $this>mTotalPagesNum$this>mIndexBarLength+1;
-
- }
-
- else {
-
- $first_number = $this>mCurPageNumber$offset;
-
- }
-
- $last_number = $first_number+$this>mIndexBarLength1;
-
- }
-
- $last_number;
-
- for( $i=$first_number; $i<=$last_number; $i++ ) {
-
- if( $this>mCurPageNumber == $i ) {
-
- $page_index .= "".$i." ";
-
- }
-
- else {
-
- $page_index .= "".$i." ";
-
- }
-
- }
-
- return $page_index;
-
- }
-
-
-
- private function SetIndexBar() {
-
- $this>mIndexBar = $this>GetNavFirstButton();
-
- $this>mIndexBar .= $this>GetNavPreButton();
-
- $this>mIndexBar .= $this>GetPageIndex();
-
- $this>mIndexBar .= $this>GetNavNextButton();
-
- $this>mIndexBar .= $this>GetNavLastButton();
-
- }
-
-
-
- private function GetNavFirstButton() {
-
- return "".$this>mFirstButton." ";
-
- }
-
-
-
- private function GetNavPreButton() {
-
- if( $this>mCurPageNumber>1 ) {
-
- $pre_number = $this>mCurPageNumber1;
-
- }
-
- else {
-
- $pre_number = 1;
-
- }
-
- return "".$this>mPreButton." ";
-
- }
-
-
-
- private function GetNavNextButton() {
-
- if( $this>mCurPageNumbermTotalPagesNum ) {
-
- $next_number = $this>mCurPageNumber+1;
-
- }
-
- else {
-
- $next_number = $this>mTotalPagesNum;
-
- }
-
- return "".$this>mNextButton." ";
-
- }
-
-
-
- private function GetNavLastButton() {
-
- return "".$this>mLastButton." ";
-
- }
-
-
-
- private function SetPageInfo() {
-
- $this>mPageInfo ="";
-
- $this>mPageInfo .= "共 ".$this>mTotalRowsNum." 条信息 | ";
-
- $this>mPageInfo .= "".$this>mPageRowsNum." 条/页 | ";
-
- $this>mPageInfo .= "共 ".$this>mTotalPagesNum." 页 | ";
-
- $this>mPageInfo .= "第 ".$this>mCurPageNumber." 页";
-
- $this>mPageInfo .= "";
-
- }
-
-
-
- public function GetIndexBar() {
-
- return $this>mIndexBar;
-
- }
-
-
-
- public function GetPageInfo() {
-
- return $this>mPageInfo;
-
- }
- }
- ?>
代码用法1: 配合数据库使用(例子中配合的是我自己的数据库操作类和模版类)
-
-
- $sql = "select * from member";
-
-
-
- require_once("pagebreak.php");
-
-
-
-
-
-
-
-
-
- $pagebreak = new PageBreak($sql, 20);
-
-
-
- $navbar = $pagebreak>GetPageInfo().$pagebreak>GetIndexBar();
-
-
-
- $result = $db>GetFieldsArray($sql)
-
-
-
- var_dump($result);
-
-
-
- echo $navbar;
代码用法2: 配合要输出的数组
-
-
- $sql = "select * from member";
-
- $result = $db>GetFieldsArray($sql);
-
-
-
- require_once("pagebreak.php");
-
-
-
-
-
-
-
-
-
- $pagebreak = new PageBreak($result, 20);
-
-
-
- $navbar = $pagebreak>GetPageInfo().$pagebreak>GetIndexBar();
-
-
-
- var_dump($result);
-
-
-
- echo $navbar;
下面是输出样式:
前半部分信息条,是$pagebreak>GetPageInfo()
后半部分分页索引导航,是$pagebreak>GetIndexBar()
输出内容和样式可以很方便的在类中调整,很简单,有兴趣可以研究一下 |