Throw a paging class with higher functions (for PHP5.x)

Source: Internet
Author: User




Throw a paging class with higher functions (for PHP5.x)
Solution
I am afraid that the level is not high, so I have never let go of any code. I have been using this class for a long time. Recently, I have used the object-oriented method to rewrite it. It is applicable to PHP5.x. I am not afraid to laugh at it.
This class is applicable to the paging of database queries and array pages. The following describes how to use it.

/*

* Name: Paging class

* Introduction: Applicable to array paging and paging with SQL queries

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

* Creation Time: 20060218

* Last modification: 20070524

*/

Class PageBreak {

Private $ mTotalRowsNum = 0; // total number of rows

Private $ mCurPageNumber = 1; // current page

Private $ mTotalPagesNum = 1; // total number of pages

Private $ mQueryString; // data transmitted on the page (url? String)

Private $ mPageRowsNum = 20; // number of lines per page

Private $ mIndexBarLength = 5; // Number of index entries

Private $ mIndexBar = ''; // page number index

Private $ mPageInfo = ''; // page information

// Page number index bar style

Private $ mNextButton = "8 ";

Private $ mPreButton = "7 ";

Private $ mFirstButton = "9 ";

Private $ mLastButton = ":";

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

Private $ mCssIndexBarPage = '';

// Pagination Style

Private $ mCssPageInfoNumFont = 'color: # ff0000 ';

Private $ mCssPageInfoFont = '';



// Constructor

Public function _ construct (& $ rSqlQuery, $ userPageRowsNum = ''){

If (! Is_array ($ rSqlQuery )){

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

}

Else {

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

}

}



// Set database 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 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 ();

}

}



// Total number of database-type computing rows

Private function SetDbTotalRowsNum ($ rSqlQuery ){

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

}



// Total number of rows in array Calculation

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'];

}

}



// Corrected the SQL truncation statement

Private function SetSqlQuery (& $ rSqlQuery ){

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

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

}



// Corrected the intercepted Array

Private function SetArray (& $ rArray ){

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

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

}



// Modify $ _ GET to pass 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 = ";

}

}



// Set page number index entries

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. = "mCssIndexBarCurPage." '> ". $ I ."";

}

Else {

$ Page_index. = "mQueryString. $ I." 'style = '". $ this> mCssIndexBarPage."'> ". $ I ."";

}

}

Return $ page_index;

}



// Set page number index entries

Private function SetIndexBar (){

$ This> mIndexBar = $ this> GetNavFirstButton ();

$ This> mIndexBar. = $ this> GetNavPreButton ();

$ This> mIndexBar. = $ this> GetPageIndex ();

$ This> mIndexBar. = $ this> GetNavNextButton ();

$ This> mIndexBar. = $ this> GetNavLastButton ();

}



// Get the page number index homepage button

Private function GetNavFirstButton (){

Return "mQueryString." 1 '> ". $ this> mFirstButton ."";

}



// Obtain the page number index entry previous page button

Private function GetNavPreButton (){

If ($ this> mCurPageNumber> 1 ){

$ Pre_number = $ this> mCurPageNumber1;

}

Else {

$ Pre_number = 1;

}

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

}



// Obtain the next page of the page number index

Private function GetNavNextButton (){

If ($ this> mCurPageNumber <$ this> mTotalPagesNum ){

$ Next_number = $ this> mCurPageNumber + 1;

}

Else {

$ Next_number = $ this> mTotalPagesNum;

}

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

}



// Get the last page button of the page number index

Private function GetNavLastButton (){

Return "mQueryString. $ this> mTotalPagesNum." '> ". $ this> mLastButton ."";

}



// Set paging information

Private function SetPageInfo (){

$ This> mPageInfo = "mCssPageInfoFont." '> ";

$ This> mPageInfo. = "mCssPageInfoNumFont." '> ". $ this> mTotalRowsNum." Information | ";

$ This> mPageInfo. = "mCssPageInfoNumFont." '> ". $ this> mPageRowsNum." entries/pages | ";

$ This> mPageInfo. = "mCssPageInfoNumFont." '> ". $ this> mTotalPagesNum." Page | ";

$ This> mPageInfo. = "mCssPageInfoNumFont." '> ". $ this> mCurPageNumber." Page ";

$ This> mPageInfo. = "";

}



// Retrieve the page number index

Public function GetIndexBar (){

Return $ this> mIndexBar;

}



// Retrieve the page information

Public function GetPageInfo (){

Return $ this> mPageInfo;

}



}

?>


Copy code usage 1: used with the database (in this example, I used my own database operation class and template class) // This is an SQL query statement. We will paging its query results.

$ SQL = "select * from member ";



// Read the paging class

Require_once ("pagebreak. php ");



// Page Initialization

// $ SQL is the preceding query statement

// 20 is the number displayed on each page

// Through page class initialization, this query statement is added with "limit ......"

$ Pagebreak = new PageBreak ($ SQL, 20 );



// Generate a paging index navigation bar

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



// Query result (use my own class here, not to mention)

$ Result = $ db> GetFieldsArray ($ SQL)



// Output query results

Var_dump ($ result );



// Output the paging index navigation bar

Echo $ navbar;


Copy code usage 2: match the array to be output // This is an SQL query statement and get the query result

$ SQL = "select * from member ";

$ Result = $ db> GetFieldsArray ($ SQL );



// Read the paging class

Require_once ("pagebreak. php ");



// Page Initialization

// $ Result is the result obtained after the preceding query.

// 20 is the number displayed on each page

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

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



// Generate a paging index navigation bar

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



// Output query results

Var_dump ($ result );



// Output the paging index navigation bar

Echo $ navbar;


Copy the output style below the code,
The first half of the message is $ pagebreak> GetPageInfo ()
The second half of the page index navigation is $ pagebreak> GetIndexBar ()



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

[]
Attachment: you cannot download or view attachments in your user group.

D8888D reply content
Good stuff, why don't you pack it for easy learning: $
D8888D reply content
This is similar to the news list page. Not the news content page... 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 page of the content ..

[]
D8888D reply content
[Img] http://www.phpchina.com/bbs/images/smilies/default/policy.gif#/img] [img] Quit
D8888D reply content
Very good
Code available
Annotation
There are Application Instances

We hope you can use this form to send code.
D8888D reply content
:):):):)
D8888D reply content
The question is, where is ordy by... written?
1.
D8888D reply content
Why can't I output a paging navigation... can I give a detailed introduction! I'm a newbie!
D8888D reply content
How can we do this :(
D8888D reply content
Original Post published by orclord at 200761 [url = http://www.phpchina.com/bbs/redirect.php? Goto = findpost & pid = 201879 & ptid = 26485] link mark [img] http://www.phpchina.com/bbs/images/common/back.gif?/img=#/url]
The question is, where is ordy by... written?
1.
Except the limit... information, others are in $ SQL.
The SetSqlQuery method can automatically add limit... content to $ SQL statements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.