Php paging original and paging class _ PHP Tutorial

Source: Internet
Author: User
Original and paging classes in php. The so-called paging display refers to dividing the result set in the database into segments for display. here two initial parameters are required: how many records per page ($ PageSize )? Currently, it is called paging display, that is, the result set in the database is divided into segments for display. here two initial parameters are required: how many records per page ($ PageSize )?

What is the current page ($ CurrentPageID )?
Now, you only need to give me another result set, so that I can display a specific result.
Other parameters, such as the previous page ($ PReviousPageID), Next page ($ NextPageID), and total page ($ numPages), can all be obtained based on the front.
Take the MySQL database as an example. if you want to extract some content from the table, you can use the SQL statement: select * from table limit offset, rows. Take a look at the following SQL statements and try to find the rule rate.
First 10 records: select * from table limit 0, 10
11th to 20 records: select * from table limit 10, 10
21st to 30 records: select * from table limit 20, 10
......
This set of SQL statements is the SQL statement used to retrieve data of each page in the table when $ PageSize = 10. we can summarize this template:
Select * from table limit ($ CurrentPageID-1) * $ PageSize, $ PageSize
Use this template to compare the corresponding values with the preceding SQL statements to see if this is the case. After solving the most important problem of how to obtain data, the rest is simply passing parameters, constructing appropriate SQL statements, and then using php to obtain data from the database and display it. I will describe the code below.
3. simple code
Please read the following code in detail and debug and run it once. you 'd better modify it once and add your own functions, such as search.
[Php]

The code is as follows:

// Establish a database connection
$ Link = mysql_connect ("localhost", "mysql_user", "mysql_passWord ")
Or die ("cocould not connect:". mysql_error ());
// Obtain the current page number
If (isset ($ _ GET ['Page']) {
$ Page = intval ($ _ GET ['Page']);
}
Else {
$ Page = 1;
}
// Number of pages per page
$ PageSize = 10;
// Obtain the total data volume
$ SQL = "select count (*) as amount from table ";
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_row ($ result );
$ Amount = $ row ['amount'];
// Count the total number of pages
If ($ amount ){
If ($ amount <$ page_size) {$ page_count = 1;} // if the total data volume is less than $ PageSize, there is only one page
If ($ amount % $ page_size) {// Retrieve the total data volume divided by the remainder of each page
$ Page_count = (int) ($ amount/$ page_size) + 1; // if there is a remainder, the number of pages equals the total data volume divided by the result of each page number and then adds one
} Else {
$ Page_count = $ amount/$ page_size; // if there is no remainder, the number of pages equals the total data volume divided by the result of each page
}
}
Else {
$ Page_count = 0;
}
// Flip link
$ Page_string = ";
If ($ page = 1 ){
$ Page_string. = 'Page 1 | previous page | ';
}
Else {
$ Page_string. = 'Page 1 | previous page | ';
}
If ($ page = $ page_count) | ($ page_count = 0 )){
$ Page_string. = 'next page | last page ';
}
Else {
$ Page_string. = 'next page | last page ';
}
// Obtain data and return results in two-dimensional array format
If ($ amount ){
$ SQL = "select * from table order by id desc limit". ($ page-1) * $ page_size. ", $ page_size ";
$ Result = mysql_query ($ SQL );

While ($ row = mysql_fetch_row ($ result )){
$ Rowset [] = $ row;
}
} Else {
$ Rowset = array ();
}
// The code that does not contain the display result is not in the scope of the discussion. you only need to use foreach to easily display the result using the two-dimensional array.
?>

Create a PHP page splitter and automatically generate the page number. JS calls the function.

The code is as follows:

Class PageView {
/** Page number **/
Public $ pageNo = 1;
/** Page size **/
Public $ pageSize = 20;
/** Total number of pages **/
Public $ pageCount = 0;
/** Total number of records **/
Public $ totalNum = 0;
/** Offset, starting line of the current page **/
Public $ offSet = 0;
/** Data per page **/
Public $ pageData = array ();

/** Whether the previous page exists **/
Public $ hasPrePage = true;
/** Whether the next page exists **/
Public $ hasNextPage = true;

Public $ pageNoList = array ();

Public $ jsFunction = 'jsfunction ';
/**
*
* @ Param unknown_type $ count total number of rows
* @ Param unknown_type $ size page size
* @ Param unknown_type $ string
*/
Public function _ construct ($ count = 0, $ size = 20, $ pageNo = 1, $ pageData = array (), $ jsFunction = 'jsfunction '){

$ This-> totalNum = $ count; // total number of records
$ This-> pageSize = $ size; // size of each page
$ This-> pageNo = $ pageNo;
// Calculate the total number of pages
$ This-> pageCount = ceil ($ this-> totalNum/$ this-> pageSize );
$ This-> pageCount = ($ this-> pageCount <= 0 )? 1: $ this-> pageCount;
// Check pageNo
$ This-> pageNo = 0? 1: $ this-> pageNo;
$ This-> pageNo = $ this-> pageNo> $ this-> pageCount? $ This-> pageCount: $ this-> pageNo;

// Calculate the offset
$ This-> offset = ($ this-> pageNo-1) * $ this-> pageSize;
// Check whether the previous page is displayed
$ This-> hasPrePage = $ this-> pageNo = 1? False: true;

$ This-> hasNextPage = $ this-> pageNo >=$ this-> pageCount? False: true;

$ This-> pageData = $ pageData;
$ This-> jsFunction = $ jsFunction;

}
/**
* Paging algorithm
* @ Return
*/
Private function generatePageList (){
$ PageList = array ();
If ($ this-> pageCount <= 9 ){
For ($ I = 0; $ I <$ this-> pageCount; $ I ++ ){
Array_push ($ pageList, $ I + 1 );
}
} Else {
If ($ this-> pageNo <= 4 ){
For ($ I = 0; $ I <5; $ I ++ ){
Array_push ($ pageList, $ I + 1 );
}
Array_push ($ pageList,-1 );
Array_push ($ pageList, $ this-> pageCount );

} Else if ($ this-> pageNo> $ this-> pageCount-4 ){
Array_push ($ pageList, 1 );

Array_push ($ pageList,-1 );
For ($ I = 5; $ I> 0; $ I --){
Array_push ($ pageList, $ this-> pageCount-$ I + 1 );
}
} Else if ($ this-> pageNo> 4 & $ this-> pageNo <= $ this-> pageCount-4 ){
Array_push ($ pageList, 1 );
Array_push ($ pageList,-1 );

Array_push ($ pageList, $ this-> pageNo-2 );
Array_push ($ pageList, $ this-> pageNo-1 );
Array_push ($ pageList, $ this-> pageNo );
Array_push ($ pageList, $ this-> pageNo + 1 );
Array_push ($ pageList, $ this-> pageNo + 2 );

Array_push ($ pageList,-1 );
Array_push ($ pageList, $ this-> pageCount );

}
}
Return $ pageList;
}

/***
* Create a paging control
* @ Param
* @ Return String
*/
Public function echoPageAsDiv (){
$ PageList = $ this-> generatePageList ();

$ PageString ="

";

If (! Empty ($ pageList )){
If ($ this-> pageCount> 1 ){
If ($ this-> hasPrePage ){
$ PageString = $ pageString. "jsFunction." (". ($ this-> pageNo-1).") "> Previous Page ";
}
Foreach ($ pageList as $ k => $ p ){
If ($ this-> pageNo = $ p ){
$ PageString = $ pageString. "". $ this-> pageNo ."";
Continue;
}
If ($ p =-1 ){
$ PageString = $ pageString ."...";
Continue;
}
$ PageString = $ pageString. "jsFunction." (". $ p.") ">". $ p ."";
}

If ($ this-> hasNextPage ){
$ PageString = $ pageString. "jsFunction." (". ($ this-> pageNo + 1).") "> Next Page ";
}

}
}
$ PageString = $ pageString .("

");
Return $ pageString;
}
}

?>

PHP call

The code is as follows:


$ PageNo = $ _ GET ['pageno'];
If (empty ($ pageNo )){
}
// Paging data
$ PageData = News: getNewsPage ($ pageNo, $ pageSize );
// Obtain the total number of rows
$ Count = News: getNewsCount ();
// Create a page splitter
$ P = new PageView ($ count ['0'] ['total'], $ pageSize, $ pageNo, $ pageData );
// Generate page number
$ PageViewString = $ p-> echoPageAsDiv ();

Aggregate ($ PageSize )? Current is...

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.