PHP pagination collection _ php instance

Source: Internet
Author: User
This article brings together several useful php paging classes, all of which have been tested by many netizens and can be used directly by our friends. Paging Class 1

The Code is as follows:


<? Php
/**
Paging type
Modify: Silence
Creatdate: 2006-5-30
LastModify: 2009-5-31
Usage
$ Page = new page ($ result, 20); // $ result indicates the number of returned record sets, and 20 indicates the number of returned entries per page.
$ Index = $ page-> GetIndexBar (). $ page-> GetPageInfo ();
Print_r ($ result );
Echo \"

\";
Echo \"

\ ". $ Index .\" \";
*/
Class Page {
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 = 11; // 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 = \ "font-weight: 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-> mTotalRowsNum-1)/$ this-> mPageRowsNum) + 1 );
}
// Calculate the current page number
Private function SetCurPageNumber (){
If ($ _ GET ['page']) {
$ This-> mCurPageNumber = $ _ GET ['page'];
}
}
// Corrected the SQL truncation statement
Private function SetSqlQuery (& $ rSqlQuery ){
$ Start_number = ($ this-> mCurPageNumber-1) * $ this-> mPageRowsNum;
$ RSqlQuery. = \ "LIMIT \". $ start_number. \ ", \". $ this-> mPageRowsNum;
}
// Corrected the intercepted Array
Private function SetArray (& $ rArray ){
$ Start_number = ($ this-> mCurPageNumber-1) * $ 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 = \"? Page = \";
} Else {
$ This-> mQueryString = preg_replace (\"/&? Page = \ d +/\ ",'', $ query_string );
$ This-> mQueryString = \"? \ ". $ This-> mQueryString. \" & 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-> mIndexBarLength-1;
}
$ 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-> mCurPageNumber-1;
} 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. \" message | \";
$ 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;
}
// Release class
Function _ destruct (){
}
}
?>

Paging type 2

The Code is as follows:


<? Php
/*
* Created on 2007-6-8
* Programmer: Alan, Msn-haowubai@hotmail.com
* KeBeKe.com Develop a project PHP-MySQL-Apache
* Window-Preferences-PHPeclipse-PHP-Code Templates
*/
// To avoid errors caused by repeated file inclusion, the following conditions are added to determine whether a function exists:
If (! Function_exists (pageft )){
// Define the pageft () function. The three parameters are described as follows:
// $ Totle: total information;
// $ Displaypg: Number of information displayed on each page. The default value is 20;
// $ Url: the link in the paging navigation. Except for adding different Query Information "page", the link is the same as the URL.
// The default value should be the URL of the current page (that is, $ _ SERVER ["REQUEST_URI"]), but the default value can only be a constant on the right, so this default value is set to an empty string, set this page URL in the function.
Function pageft ($ totle, $ displaypg = 20, $ url = ''){
// Define several global variables:
// $ Page: Current page number;
// $ Firstcount: (database) The start item of the query;
// $ Pagenav: page navigation bar code, which is not output in the function;
// $ _ SERVER: required to read the URL "$ _ SERVER [" REQUEST_URI "]" on this page.
Global $ page, $ firstcount, $ pagenav, $ _ SERVER;
// To enable external function access to "$ displaypg", set it as a global variable. Note that after a variable is redefined as a global variable, the original value is overwritten, so the value is assigned again here.
$ GLOBALS ["displaypg"] = $ displaypg;
If (! $ Page) $ page = 1;
// If $ url uses the default value, that is, null value, the value is assigned to the URL of the current page:
If (! $ Url) {$ url = $ _ SERVER ["REQUEST_URI"];}
// URL analysis:
$ Parse_url = parse_url ($ url );
$ Url_query = $ parse_url ["query"]; // retrieve the query string of the URL separately.
If ($ url_query ){
// Because the URL may contain page number information, we need to remove it to add new page number information.
// The regular expression is used here. See "Regular Expressions in PHP"
$ Url_query = ereg_replace ("(^ | &) page = $ page", "", $ url_query );
// Replace the query string of the processed URL with the query string of the original URL:
$ Url = str_replace ($ parse_url ["query"], $ url_query, $ url );
// Add page after the URL to query the information, but the value to be assigned is:
If ($ url_query) $ url. = "& page"; else $ url. = "page ";
} Else {
$ Url. = "? Page ";
}
// Page number calculation:
$ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total number of pages
$ Page = min ($ lastpg, $ page );
$ Prepg = $ page-1; // Previous page
$ Nextpg = ($ page = $ lastpg? 0: $ page + 1); // next page
$ Firstcount = ($ page-1) * $ displaypg;
// Start the paging navigation bar code:
$ Pagenav = "display". ($ Totle? ($ Firstcount + 1): 0 )."-". Min ($ firstcount + $ displaypg, $ totle )."A total of $ totle records ";
// If there is only one page, the function will jump out:
If ($ lastpg <= 1) return false;
$ Pagenav. = "Homepage ";
If ($ prepg) $ pagenav. = "Previous Page"; else $ pagenav. = "Previous Page ";
If ($ nextpg) $ pagenav. = ""; else $ pagenav. = "";
$ Pagenav. = "last page ";
// Pull-down Jump list, listing all page numbers cyclically:
$ Pagenav. ="\ N ";For ($ I = 1; $ I <= $ lastpg; $ I ++ ){If ($ I = $ page) $ pagenav. ="$ I\ N ";Else $ pagenav. ="$ I\ N ";}$ Pagenav. ="Page, total $ lastpg page ";
}
}

// ------------------------- Demo -------------------------------
/*
// (Previous program omitted)
Include ("pageft. php"); // contains the "pageft. php" file.
// Obtain the total number of information
$ Result = mysql_query ("select * from mytable ");
$ Total = mysql_num_rows ($ result );
// Call pageft () to display 10 messages per page (this parameter can be omitted when the default value is 20). Use the URL on this page (which is omitted by default ).
Pageft ($ total, 10 );
// The global variables generated now come in handy:
$ Result = mysql_query ("select * from mytable limit $ firstcount, $ displaypg ");
While ($ row = mysql_fetch_array ($ result )){
// (List content omitted)
}
// Output the pagination navigation bar code:
Echo $ pagenav;
// (Subsequent procedures)
*/
?>

Paging Series 3

The Code is as follows:


<? Php
Class Page {
Private $ total; // query the total number of data records
Private $ page; // current page
Private $ num; // number of records per page
Private $ pageNum; // total number of pages
Private $ offset; // number of start offsets of records retrieved from the database
Function _ construct ($ total, $ page = 1, $ num = 5 ){
$ This-> total = $ total;
$ This-> page = $ page;
$ This-> num = $ num;
$ This-> pageNum = $ this-> getPageNum ();
$ This-> offset = $ this-> getOffset ();
}
Private function getPageNum (){
Return ceil ($ this-> total/$ this-> num );
}
Private function getNextPage (){
If ($ this-> page ==$ this-> pageNum)
Return false;
Else
Return $ this-> page + 1;
}
Private function getPrevPage (){
If ($ this-> page = 1)
Return false;
Else
Return $ this-> page-1;
}
// Database query offset
Private function getOffset (){
Return ($ this-> page-1) * $ this-> num;
}
// Number of records starting from the current page
Private function getStartNum (){
If ($ this-> total = 0)
Return 0;
Else
Return $ this-> offset + 1;
}
// Number of records ending on the current page
Private function getEndNum (){
Return min ($ this-> offset + $ this-> num, $ this-> total );
}
Public function getPageInfo (){
$ PageInfo = array (
"Row_total" => $ this-> total,
"Row_num" => $ this-> num,
"Page_num" => $ this-> getPageNum (),
"Current_page" => $ this-> page,
"Row_offset" => $ this-> getOffset (),
"Next_page" => $ this-> getNextPage (),
"Prev_page" => $ this-> getPrevPage (),
"Page_start" => $ this-> getStartNum (),
"Page_end" => $ this-> getEndNum ()
);
Return $ pageInfo;
}
}
?>

The three types of paging classes are both efficient and easy to use. We recommend them to you here.

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.