Php Generic paging code _ PHP Tutorial

Source: Internet
Author: User
Php Generic paging code. Php Generic paging code this is a php paging code used. we summarize the special paging code and write a php Generic paging class which is easy to use and easy to understand. Php Generic paging code this is a php paging code used. we summarize the special paging code and write a php Generic paging class which is easy to use and easy to understand.

Php Tutorial generic paging code
This is a php paging code for usage. we have summarized the special paging code and wrote a php Generic paging class which is easy to use and easy to understand.
*/

Class pidepage {// paging class
Private $ total; // The total number of records to be displayed
Private $ url; // The requested url
Private $ displaypg; // number of records displayed on each page. by default, 10 records are displayed on each page.
Private $ page; // The current page number.
Private $ lastpg; // The total number of pages, that is, the page number of the last page
Private $ prepg; // Previous Page
Private $ nextpg; // Next page
Private $ firstcount; // The sequence number starting from 0
Private $ startd; // The number of records starting with the number of records.
Private $ stopd; // The number of records whose number ends.

// Constructor
Public function _ construct ($ url, $ total, $ displaypg ){
$ This-> url = $ url; // The requested url
$ This-> total = $ total; // total number of records
// If ($ displaypg = '')
$ This-> displaypg = $ displaypg; // number of records displayed on each page
$ This-> initpidepage (); // initialize the paging class
// Echo ','. $ this-> displaypg;
}

// Initialize the paging class
Private function initpidepage (){
// Analyze the url
$ Parse_url = parse_url ($ this-> url); // interpret the url as an array with fixed key-value pairs
$ Url_query = $ parse_url ['query']; // retrieves the query string from the url.
If ($ url_query) {// if a query string exists, delete the query field on the current page of the query string, for example, & page = $ page or page = $ page.
Ereg ('(^ | &) page = ([0-9] *)', $ url_query, $ k );
$ This-> page = $ k [2]; // get the value of the current page
$ Url_query = ereg_replace ("(^ | &) page = $ this-> page", '', $ url_query); // delete the query fields on the current page of the query string, for example: & page = $ page or page = $ page
$ This-> url = str_replace ($ parse_url ['query'], $ url_query, $ this-> url); // retain other query strings,
$ This-> page = $ this-> page? $ This-> page: 1; // w if the query string does not contain the value of the current page, set the current page to 1.
If ($ url_query) {// if there are other query strings, add the paging query string in the form of & page = $ page
$ This-> url. = '& page ';
} Else {// if no other query string exists, the paging query string is added in the form of page = $ page.
$ This-> url. = 'page ';
}
} Else {// if no query string exists, add? Page = $ page-based paging query string
$ This-> page = 1;
$ This-> url. = '? Page ';
}
$ This-> lastpg = ceil ($ this-> total/$ this-> displaypg); // calculate the total number of pages, that is, the page number of the last page
$ This-> page = min ($ this-> lastpg, $ this-> page); // if the current page is greater than the total number of pages, the current page is the last page
$ This-> prepg = $ this-> page-1; // the previous page is the current page minus one www. bKjia. c0m.
$ This-> nextpg = $ this-> page + 1; // ($ this-> page = $ this-> lastpg )? $ This-> lastpg: ($ this-> page + 1); // the next page is the current page plus one. if the current page is the last page, the next page is 0.
$ This-> firstcount = ($ this-> page-1) * $ this-> displaypg; // calculates the number of records starting with the number of records on the current page, starting from 0.
$ This-> startd = $ this-> total? ($ This-> firstcount + 1): 0; // record start number starts from 1
$ This-> stopd = min ($ this-> firstcount + $ this-> displaypg, $ this-> total); // record end number
// Echo $ this-> displaypg;
// Echo $ this-> nextpg. '+ ='. $ this-> lastpg;
}

Public function getpageinfo () {// obtain the basic information of the current page, for example, display 1st-10 records, a total of 23 records.
Return 'display Records '. $ this-> startd.'-'. $ this-> stopd.', total Records '. $ this-> total. ';
}

Public function getcommonpagenav () {// Obtain the usual paging navigation, for example, the last page of the previous page on the homepage
$ Commonnav = '';
If ($ this-> lastpg = 1) {// if there is only one page, return to the paging navigation, exit, do not display the next page, previous page, and so on...
Return $ commonnav;
Break;
}
$ Commonnav = 'URL. '= 1 "class =" compagestyle "> homepage'; // Set the homepage navigation, page = 1
If ($ this-> prepg ){
$ Commonnav. = 'URL. '='. $ this-> prepg. '"class =" compagestyle "> ';
} Else {
$ Commonnav. = 'previous page ';
}
If ($ this-> nextpg <= $ this-> lastpg ){
$ Commonnav. = 'URL. '='. $ this-> nextpg. '"class =" compagestyle "> ';
} Else {
$ Commonnav. = 'next page ';
}
$ Commonnav. = 'URL. '='. $ this-> lastpg. '"class =" compagestyle "> end page'; // display the link to the end page
Return $ commonnav;
}

// Retrieve the navigation page, for example, page n
Public function getjumppagenav (){
//$ Jumpnav = 'to the nthUrl. '= "+ this. value'>'." n ";
For ($ I = 1; $ I <= $ this-> lastpg; $ I ++ ){
If ($ I = $ this-> page) {// use the current page number as the default option.
$ Jumpnav. =''. $ I .''. "N ";
} Else {
$ Jumpnav. =''. $ I .''. "N ";
}
}
$ Jumpnav. ='Page, Total '. $ this-> lastpg.' page ';
Return $ jumpnav;
}

// Retrieve all paging navigation
Public function getallpagenav (){
$ Temp = $ this-> getpageinfo (). $ this-> getcommonpagenav (). $ this-> getjumppagenav ();
Return $ temp;
}

// Obtain the record to be displayed on the current page, which is limited in the database tutorial, for example, 0-9
Public function getlimitstr (){
// Echo $ this-> page;
// Echo $ this-> firstcount;

// Echo $ this-> dispalypg;
$ Temp = $ this-> firstcount. ','. $ this-> displaypg;
// Echo $ temp;
Return $ temp;
}

}

/*
Instance used:

* $ Result = mysql tutorial _ query ("select * from tb_pagetest"); // query the data to be displayed from the database
* $ Total = mysql_num_rows ($ result); // The total number of queried data records
* $ Pagesize = 5; // number of records displayed on each page
* $ Url = $ _ server ['request _ uri ']; // The requested uri
*
* $ Pidepageclass = new pidepage ($ url, $ total, $ pagesize); // Create a paging class (Class can be automatically initialized)
* $ Limitstr = $ pidepageclass-> getlimitstr (); // gets the start sequence number of the record to be displayed on the current page and the number of entries displayed on each page, for example: 0, 5 (Display 5 records starting from 0)
* Echo $ pidepageclass-> getallpagenav (); // display all paging navigation entries,
* For example, 13 records are displayed for the 11-13 records. Homepage previous page next page end page to * 1st page, 3 pages in total
* $ SQL = 'select * from tb_pagetest limit '. $ limitstr;
* $ Result = mysql_query ($ SQL); // Obtain the record set to be displayed on the current page from the database, and then click OK.
* For example:
* While ($ row = mysql_fetch_array ($ result ))
* Echo" ". $ Row [title]." | ". $ row [author];

....

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.