Php paging functions [classic]-PHP source code

Source: Internet
Author: User
Tags table definition
Ec (2); & lt ;? Php * & nbsp; paging Class & nbsp; designbyxqbar & nbsp; qq: 174171262 & nbsp; email: wxddong@163.com & nbsp; & nbsp; blog: www.xqbar.com & nbsp; input parameter & nbsp; si script ec (2); script


/*
Paging type
Design by xqbar
Qq: 174171262
Email: wxddong@163.com
Blog: http://www.xqbar.com
Input parameters
Size (number of records per page)
Url (connection address)
Style)
SQL (run Statement)
*/
Class page {
Public $ SQL = ''; // paging SQL statement
Public $ url = ''; // the address of the next page passed by page. You can add parameters to search and use them.
Public $ size = 10; // by default, 10 data entries are displayed on each page.
Public $ style = 1; // default paging display style
Private $ totalsize = 0; // total data volume
Private $ totalpage = 0; // total number of pages
Private $ page = 1; // current page
Private $ link; // database connection point
Private $ flag = false; // initialization flag
Private $ offset = 0; // The page offset value limit $ offset, $ size;
// Constructor transmits the database connection point
Function _ construct ($ link ){
$ This-> page = (isset ($ _ GET ['page']) &! Empty ($ _ GET ['page']) & (int) ($ _ GET ['page'])? (Int) $ _ GET ['page']: 1; // obtain the current page parameter value to determine the page number.
$ This-> link = $ link;
}
// Destructor
Function _ destruct (){
@ Mysql_close ($ this-> link );
$ This-> link = NULL;
}
// Constructor Function page ($ link ){
$ This->__ construct ($ link );
}
// Initialization
Function init (){
If (empty ($ this-> SQL) {$ this-> halt ("error warning: SQL statement cannot be blank! "); Exit () ;}// determines whether the SQL exists
$ This-> SQL = str_replace (SQL); // "> '@ _', DB_PREFIX, $ this-> SQL); // Replace the table prefix in SQL
$ This-> size = ($ this-> size <= 0 )? 10: $ this-> size; // the initial number of data entries per page.
$ This-> page = ($ this-> page <1 )? 1: $ this-> page; // current page number
# Optimize the total number of query records after the first page
// Here I used the first page view to query the database to obtain the total number of databases. When I click the next page, the database will not be queried. This may save some time (I think) when there is a large amount of data)
If ($ this-> page = 1 &&! Isset ($ _ GET ['tsize']) {
SQL "> $ result = @ mysql_query ($ this-> SQL );
$ This-> totalsize = @ mysql_num_rows ($ result );
@ Mysql_free_result ($ result );
} Else {
// The only drawback of the problem above is that the page is no longer correct after you manually change the tsize value in the address bar
$ This-> totalsize = (int) $ _ GET ['tsize'];
}
$ This-> url = (empty ($ this-> url ))? $ _ SERVER ['php _ SELF '].'? Tsize = '. $ this-> totalsize: $ this-> url.' & tsize = '. $ this-> totalsize; // pass totalsize to the next page
$ This-> totalpage = ceil ($ this-> totalsize/$ this-> size); // obtain the total number of pages
$ This-> page = ($ this-> page> $ this-> totalpage )? $ This-> totalpage: $ this-> page;
$ This-> offset = ($ this-> page-1) * $ this-> size; // get the offset Value
}
// Query the results returned by the database to a two-dimensional array
Function fetch_array ($ result_type = MYSQL_BOTH ){
If (! $ This-> flag) {$ this-> init (); $ this-> flag = true ;}
If ($ this-> totalsize <= 0) {return NULL ;}
$ This-> SQL. = "limit $ this-> offset, $ this-> size ";
SQL, $ this-> link "> $ result = @ mysql_query ($ this-> SQL, $ this-> link );
While ($ row = mysql_fetch_array ($ result, $ result_type) {$ rows [] = $ row ;}
@ Mysql_free_result ($ result );
Return $ rows;
}
// Obtain the total data count
Function getsize (){
If (! $ This-> flag) {$ this-init (); $ this-> flag = true;} // checks whether the initialization has been performed before this. If yes, the system returns the result directly (the same below)
Return $ this-> totalsize;
}
// Obtain the total number of pages
Function getpage (){
If (! $ This-> flag) {$ this-init (); $ this-> flag = true ;}
Return $ this-> totalpage;
}
// Output paging links based on input parameters
Function outpage (){
If (! $ This-> flag) {$ this-init (); $ this-> flag = true ;}
Switch ($ this-> style ){
#1 page x total page x previous page next page x/total page x
Case 1:
$ Pagehtml = "no.{$ This-> page}Total pages{$ This-> totalpage}Page ";
$ Pagehtml. = ($ this-> page = 1 )? "Homepage previous page": "url} & page = 1 '> homepage url} & page =". ($ this-> page-1). "'> previous page ";
$ Pagehtml. = ($ this-> page = $ this-> totalpage )? "Last page of the next page": "url} & page = ". ($ this-> page + 1 ). "'> next page url} & page ={$ this-> totalpage}'> last page ";
$ Pagehtml. = "Total{$ This-> totalsize}";
Break;
#1 homepage previous page next page last page x items/pages x pages
Case 2:
$ Pagehtml = "";
$ Pagehtml. = ($ this-> page = 1 )? "Homepage previous page": "url} & page = 1 '> homepage url} & page =". ($ this-> page-1). "'> previous page ";
$ Pagehtml. = ($ this-> page = $ this-> totalpage )? "Last page of the next page": "url} & page = ". ($ this-> page + 1 ). "'> next page url} & page ={$ this-> totalpage}'> last page ";
$ Pagehtml. = "Total{$ This-> totalsize}";
Break;
#2 1 2 3 4 5
Case 3:
$ Offset = (ceil ($ this-> page/10)-1) * 10;
$ Pagehtml = "";
If ($ offset! = 0) {$ pagehtml. = "url} & page = 1' >|< <url} & page = ". ($ this-> page-1 ). "'> <";}
For ($ I = 1; $ I <= 10; $ I ++ ){
$ N = $ I + $ offset;
If ($ n >$ this-> totalpage) {break ;}
If ($ n ==$ this-> page) {$ pagehtml. = "url} & page = $ n'>$ N";}
Else {$ pagehtml. = "url} & page = $ n'>$ N";}
}
If ($ n <$ this-> totalpage) {$ pagehtml. = "url} & page = ". ($ this-> page + 1 ). "'>>> url} & page = $ this-> totalsize' >>>| ";}
Unset ($ offset, $ I, $ n );
Break;
#1
Default:
$ Pagehtml = "page {$ this-> page} {$ this-> totalpage ";
$ Pagehtml. = ($ this-> page = 1 )? "Homepage previous page": "url} & page = 1 '> homepage url} & page =". ($ this-> page-1). "'> previous page ";
$ Pagehtml. = ($ this-> page = $ this-> totalpage )? "Last page of the next page": "url} & page = ". ($ this-> page + 1 ). "'> next page url} & page ={$ this-> totalpage}'> last page ";
Break;
}
Return $ pagehtml;
}
// Error output
Function halt ($ msg ){
Echo"";
Echo"";
Echo" \ R \ n ";
Echo"Error";
Echo"";
Echo"

";
Echo $ msg;
Echo"

";
Echo"";
}
}
?>

Require_once ('mysql. class. php ');
Require_once ('page. class. php ');
$ Db = new mysql ();
$ Link = $ db-> open ();
# Define SQL and url
$ SQL = 'select * from @ _ info'; (@ _ is the prefix of my table definition, which is replaced after the class is passed in)
$ Url = '';
$ Mypage = new page ($ link );
$ Mypage-> size = 10;
$ Mypage-> style = 3;
$ Mypage-> url = $ url;
$ Mypage-> SQL = $ SQL. "order by id desc ";
$ Ls = $ mypage-> fetch_array (MYSQL_ASSOC); // get the returned result.
If (count ($ ls) = 0 ){
Echo 'no result ';
} Else {
Foreach ($ ls as $ value ){
Echo $ value ['title'];
}
// Page number
Echo $ mypage-> outpage ();
}
?>

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.