Throw out a paging class with relatively high functionality (for PHP5.x)_PHP tutorial

WBOY
Release: 2016-07-13 10:52:03
Original
892 people have browsed it




Throw out a paging class with relatively high functionality (for PHP5.x)
Solution
I am afraid that my level is not high, so I never let go of any code. I have been using this class for a long time, and recently rewritten it using object-oriented methods, which is suitable for PHP5.
This class is suitable for paging with database queries and array paging. Here's how to use it.

/*

* Name: Pagination class

* Introduction: Suitable for array paging and paging with sql query

* Author: idlion || Moonfly ([email=id_lion@hotmail.com]id_lion@hotmail.com[/email])

* Creation time: 20060218

* Last modified: 20070524

*/

class PageBreak {

private $mTotalRowsNum = 0; //Total number of information rows

private $mCurPageNumber = 1; // Current page

private $mTotalPagesNum = 1; //Total number of pages

private $mQueryString; // Data passed by the page (string after url?)

private $mPageRowsNum = 20; //Number of rows displayed per page

private $mIndexBarLength = 5; // Number of pages of index bar

private $mIndexBar = ''; // Page index bar

private $mPageInfo = ''; // paging information

//Page index bar style

private $mNextButton = "8";

private $mPreButton = "7";

private $mFirstButton = "9";

private $mLastButton = ":";

private $mCssIndexBarCurPage = "fontweight:bold;color:#FF0000";

private $mCssIndexBarPage = '';

// Pagination information style

private $mCssPageInfoNumFont = 'color:#FF0000';

private $mCssPageInfoFont = '';



//Construction method

public function __construct(&$rSqlQuery, $userPageRowsNum='') {

if( !is_array($rSqlQuery) ) {

$this>SetDbPageBreak($rSqlQuery, $userPageRowsNum);

}

else {

$this>SetArrayPageBreak($rSqlQuery, $userPageRowsNum);

}

}



//Set database type paging

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();

}

}



//Set array type paging

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();

}

}



// Database type calculation of total number of rows

private function SetDbTotalRowsNum($rSqlQuery) {

$this>mTotalRowsNum = mysql_num_rows( mysql_query($rSqlQuery) );

}



//Array type calculation total number of rows

private function SetArrayTotalRowsNum($array) {

$this>mTotalRowsNum = count($array);

}



// Calculate the total number of pages

private function SetTotalPagesNum($userPageRowsNum='') {

if( $userPageRowsNum ) {

$this>mPageRowsNum = $userPageRowsNum;

}

$this>mTotalPagesNum = (int)( floor( ($this>mTotalRowsNum1)/$this>mPageRowsNum )+1 );

}



// Calculate the current page number

private function SetCurPageNumber() {

if( $_GET['cur_page'] ) {

$this>mCurPageNumber = $_GET['cur_page'];

}

}



// Modify Sql interception statement

private function SetSqlQuery(&$rSqlQuery) {

$start_number = ($this>mCurPageNumber1)*$this>mPageRowsNum;

$rSqlQuery .= " LIMIT ".$start_number.",".$this>mPageRowsNum;

}



// Correct the intercepted Array

private function SetArray(&$rArray) {

$start_number = ($this>mCurPageNumber1)*$this>mPageRowsNum;

$rArray = array_slice($rArray, $start_number, $this>mPageRowsNum);

}



// Fix $_GET passing data

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 mIndexBarLength ) {

$first_number = 1;

$last_number = $this>mTotalPagesNum;

}

else {

$offset = (int)floor($this>mIndexBarLength/2);

if( ($this>mCurPageNumber$offset)
$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
if( $this>mCurPageNumber == $i ) {

$page_index .= "".$i." ";

}

else {

$page_index .= "mQueryString.$i."' style='".$this>mCssIndexBarPage."'>".$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 "mQueryString."1'>".$this>mFirstButton." ";

}



// 得到页码索引条 上一页按钮

private function GetNavPreButton() {

if( $this>mCurPageNumber>1 ) {

$pre_number = $this>mCurPageNumber1;

}

else {

$pre_number = 1;

}

return "mQueryString.$pre_number."'>".$this>mPreButton." ";

}



// 得到页码索引条 下一页按钮

private function GetNavNextButton() {

if( $this>mCurPageNumbermTotalPagesNum ) {

$next_number = $this>mCurPageNumber+1;

}

else {

$next_number = $this>mTotalPagesNum;

}

return "mQueryString.$next_number."'>".$this>mNextButton." ";

}



// 得到页码索引条 末页按钮

private function GetNavLastButton() {

return "mQueryString.$this>mTotalPagesNum."'>".$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查询语句,我们来对它的查询结果作出分页

$sql = "select * from member";



// 读取分页类

require_once("pagebreak.php");



// 分页初始化

// $sql就是上面的查询语句

// 20是每页显示的数量

// Through the initialization of the paging class, this query statement is added with "limit..."

$pagebreak = new PageBreak($sql, 20);



// Generate paging index navigation bar

$navbar = $pagebreak>GetPageInfo().$pagebreak>GetIndexBar();



// Query results (I use my own class here, no more details)

$result = $db>GetFieldsArray($sql)



// Output query results

var_dump($result);



// Output paging index navigation bar

echo $navbar;


Copy code usage 2: Match the array to be output // This is a sql query statement and get the query results

$sql = "select * from member";

$result = $db>GetFieldsArray($sql);



//Read paging class

require_once("pagebreak.php");



//Paging initialization

// $result is the result obtained after the above query

// 20 is the number displayed on each page

// Through the initialization of the paging class, this result array is automatically intercepted into the information content of the corresponding page

$pagebreak = new PageBreak($result, 20);



// Generate paging index navigation bar

$navbar = $pagebreak>GetPageInfo().$pagebreak>GetIndexBar();



// Output query results

var_dump($result);



// Output paging index navigation bar

echo $navbar;


Copy the code and below is the output style,
The first half of the information bar is $pagebreak>GetPageInfo()
The second half of the paging index navigation is $pagebreak>GetIndexBar()



The output content and style can be easily adjusted in the class. It is very simple. If you are interested, you can study it

[ ]
Attachment: Your user group cannot download or view attachments

D8888D’s reply content
Great stuff, why don’t you pack it into a package for easy study:$
D8888D’s reply content
This is similar to news list paging. It is not news content paging... This is [url=http://www.phpchina.cn/bbs/viewthread.php?tid=12999] link tag http://www .phpchina.cn/bbs/viewthread.php?tid=12999[/url] is the paging of the content..

[ ]
D8888D’s reply content
[img]http://www.phpchina.com/bbs/images/smilies/default/victory.gif[/img] [img]http://www.phpchina.com/bbs/images/smilies/default/victory .gif[/img][img]http://www.phpchina.com/bbs/images/smilies/default/victory.gif[/img]
D8888D’s reply content
Very good
There is code
There is annotation
There are application examples

I very much hope that everyone will use this form to send codes
D8888D’s reply content
:) :) :) :)
D8888D’s reply content
I have a question, it’s like this ordy by...where is it written?
If you don’t understand, ask 1
D8888D’s reply content
Why can’t the page navigation be output? Can you please introduce it in detail! I’m a newbie!
D8888D’s reply content
Why are there so many? :(
D8888D’s reply content
The original post was posted by orclord at 200761 09:43 [url=http://www.phpchina.com/bbs/redirect.php?goto=findpost&pid=201879&ptid=26485]Link tag [img]http://www.phpchina. com/bbs/images/common/back.gif[/img][/url]
I have a question, it’s like this ordy by...where is it written?
If you don’t understand, ask me
Except for the limit....information, everything else is in $sql.
The SetSqlQuery method can automatically add limit.... content to the $sql statement.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632528.htmlTechArticleThrow out a paging class (for PHP5.x) with relatively high functionality. The solution may not be high enough. So I never let go of any code. I have been using this class for a long time, and recently used object-oriented...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template