PHP class for page turning

Source: Internet
Author: User
PHP implements paging processing class PHP instance source code: PHP implements paging processing class & lt ;? PhpclassPage {var $ CountAll; total number of records var $ CountPage; number of records displayed per page var $ Link; full page information var $ ForPage; Previous Page var $ NextPage; next Page var $ PHP class for page turning

PHP instance source code: PHP class for page turning

Class Page {

Var $ CountAll; // total number of records
Var $ CountPage; // number of records displayed on each page
Var $ Link; // display the complete page information
Var $ ForPage; // Previous Page
Var $ NextPage; // Next page
Var $ FirstPage; // The first page
Var $ LastPage; // The Last Page
Var $ CurrPage; // page
Var $ PageNum; // total number of pages
Var $ Parameter; // Parameter
Var $ LimitNum; // not counting all records, but displaying some records. for example, a total of 100 records are displayed, but only the first 50 records are displayed.

Function Page ($ SQL, $ num = 30 ){

// Initialize and count the number of records
$ This-> CountPage = $ num;
Global $ mysql;
$ SQL = base64_decode ($ SQL );
$ Result = $ mysql-> Query ($ SQL );
If (0! = $ Mysql-> AffectedRows ()){
$ Row = $ mysql-> FetchArray ($ result );
$ This-> CountAll = $ row [0];
}
Else {
$ This-> CountAll = 0;
}
// Print "total $ this-> CountAll
";
}

Function ListPage ($ SQL, $ page = 0, $ SQL _all, $ other ){
// Query, define variables, and obtain data

Global $ mysql;
// Print "SQL
";
If (isset ($ this-> LimitNum) & $ this-> CountAll> $ this-> LimitNum ){
$ This-> CountAll = $ this-> LimitNum;
} // Update the total number of browsing records

$ SQL _src = $ SQL;
// If ($ page> 0 ){
$ SQL = base64_decode ($ SQL );
$ SQL _all = base64_decode ($ SQL _all );
$ SQL _src = $ SQL;
//}
// Echo $ SQL;
If ($ this-> CountAll % $ this-> CountPage) = 0) // count the total number of pages
$ Pagecount = (integer) ($ this-> CountAll/$ this-> CountPage );
Else
$ Pagecount = (integer) ($ this-> CountAll/$ this-> CountPage) + 1;
$ This-> ageNum = $ pagecount;
If ($ page> $ this-> ageNum) // if the page number exceeds the total number of pages, set it to the maximum page number.
$ Page = $ this-> ageNum;
If ($ page <= 0) // if the page number is smaller than or equal to zero, set the page number to 1.
$ Page = 1;

If ($ this-> CountAll = 0)
{
$ This-> CurrPage = 0;
} Else {
$ This-> CurrPage = $ page;
}
$ First_start = ($ page-1) * $ this-> CountPage;
$ SQL = $ SQL. "limit". $ first_start. ",". $ this-> CountPage;
// Print "2sql
";
$ Result = $ mysql-> Query ($ SQL );
If (0! = $ Mysql-> AffectedRows ()){
$ I = 0;
While ($ row = $ mysql-> FetchArray ($ result )){
$ Array [$ I] = $ row;
// Print "name:". $ array [$ I] [Name]."
";
$ I ++;
}
}

$ SQL = base64_encode ($ SQL _src );
$ SQL _all = base64_encode ($ SQL _all );
If ($ pagecount> 1 ){
If ($ page = 1 ){
$ Nextpage = $ page + 1;
$ Forpage = 1;
$ This-> Link ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> NextPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> LastPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
}
Else if ($ page> 1) & ($ page <$ pagecount )){
$ Forpage = $ page-1;
$ Nextpage = $ page + 1;
$ This-> Link ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> ForPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> NextPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> FirstPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> LastPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
}
Else if ($ page = $ pagecount ){
$ Forpage = $ page-1;
$ Nextpage = 1;
$ This-> Link ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> FirstPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
$ This-> ForPage ="Parameter. "& SQL _all =". $ SQL _all. "". $ other. "'" class = "button_a" style = "width: 30; height: 22";>";
}
}
Else {
$ This-> Link = '';
}

Return $ array; // $ array;

}

}
/***************
Example

Include ("class. config. php ");
Include ("class. mysql. php ");
Include ("class. page. php ");
Global $ mysql;
$ Config = new Config;
$ Mysql = new TDatabase ($ config );

$ Query_all = "select count (*) from user ";
$ Page_object = new Page ($ query_all, 20 );
// New Page ('number of statistical Records' statement, number of records per Page)
If (empty ($ query_page ))
$ Query_ SQL = "select * from user ";
// Note that the variable name must be $ query_ SQL $ query_page, because the connection parameter on the next page is query_ SQL query_page by default.

$ List = $ page_object-> ListPage ($ query_ SQL, $ query_page );
// ListPage)
$ Page_object-> Parameter = '& action = view ';
// This is another Parameter of the transmitted Url. if there is one Parameter, modify the variable Parameter and the system will automatically add it to the following
// Display Data
For ($ I = 0; $ I <$ page_object-> CountPage; $ I ++)
Print $ list [$ I] [ID]. "->". $ list [$ I] [UserName]."
";
// The returned data is a two-dimensional Hasse (association) array, one-dimensional is the ID number of the record, and two-dimensional is the Hasse (association) array. we recommend that you use the field name in the database to identify the value, for example, list [0] [UserName].

// Display Other Related Data
Echo $ page_object-> CountAll; // The total number of records.
Echo $ page_object-> CountPage; // The number of data entries displayed on each page
Echo $ page_object-> Link; // display the complete page information
Echo $ page_object-> FirstPage; // The first page
Echo $ page_object-> NextPage; // Next page
Echo $ page_object-> ForPage; // Previous Page
Echo $ page_object-> LastPage; // The last page.
Echo $ page_object-> CurrPage; // page
Echo $ page_object-> PageNum; // total number of pages
$ Mysql-> DatabaseClose ();
****************/

?>

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.