Php pagination using tables to pagination class _ PHP Tutorial

Source: Internet
Author: User
Php paging can use tables to paging classes. * Variables required when creating an object: $ query (SQL statement for obtaining the number of records from a data table), $ page (current page number), and $ maxline (several rows per page )) * 1. showpage method: If the above object is created * the variable required for creating the object: $ query (SQL statement for obtaining the number of records from the data table), $ page (current page number ), $ maxline (several rows per page) * 1. showpage method: if the $ query of the object created above is correct, call it directly to output the page information * 2. showtable method: required variable: $ query (the SQL statement used to read records from the database. do not add Limit because it has been added to the method) So you only need to add
The code is as follows:

/*
* Directly output data tables and paging information
* Variables required when creating an object: $ query (SQL statement for obtaining the number of records from a data table), $ page (current page number), and $ maxline (several rows per page ))
* 1. showpage method: if the $ query of the object created above is correct, call it directly to output the page information.
* 2. showtable method: required variable: $ query (SQL statement used to read records from the database. do not add Limit because it has been added to the method)
* Direct output

Is the complete table
* 3. showresult method: the $ result resource is directly returned based on the SQL statement in the submitted $ query. The table can be customized.
* Example:
// Obtain the current page and define the largest row on each page
$ Page = 1;
$ Maxline = "10 ";
If (! Empty ($ _ GET ["page"]) {
$ Page = $ _ GET ["page"];
}
// Define the SQL statement used to calculate the total number of data in a table. here, it must be the same table and condition as $ query below. create an object and output the page number and table.
$ Query = "select count (*) from mailbox ";
$ A = new PageList ($ query, $ maxline, $ page );
$ A-> showpage ();
// The list is displayed, which requires the same conditions as the preceding SQL statement.
$ Query = "select username, name, quota, created, modified, active from mailbox order by created desc ";
Echo"

";$ A-> showtable ($ query );Echo"
";
**/

Class PageList {
Private $ link;
Private $ result;
Private $ maxline;
Private $ page = 1;
Private $ startline = 0;
Private $ countline;
Public $ countpage;
Private $ prevpage;
Private $ nextpage;
// Database connection, which must be changed to your own address
Private $ dbhost = DBHOST;
Private $ dbuser = DBUSER;
Private $ dbpasswd = DBPASSWD;
Private $ dbtable = DBTABLE;

/*
* Create a database connection in the constructor
* 1. set the four database connection parameters to constant records on the config. php page.
* 2. connect to the database and select the database
* 3. set the encoding method for database execution to utf8.
* 4. assign the received $ maxline and $ page variables to the class attributes to become general attributes of the class.
* (Where $ query is a count (*) SQL statement, which is different from the query in the following method)
* 5. execute a query on the database based on the $ query statement submitted when the new object is created, and assign the total number of records to the class attribute $ this-> countline
* Set the total number of records/number of rows per page, and then use the ceil function to set the upper order to get the total number of pages and assign the value to the class attribute $ this-> countpage
* 6. based on the submitted current page number $ page, calculate the numbers of the front and back pages $ this-> prevpage and $ this-> nextpage
* You must also calculate the starting row for database reading $ this-> startline.
* There are three cases: page <2, page = last page, page> 1 (you can skip this case and use else directly)
**/
Public function _ construct ($ query, $ maxline, $ page ){
@ $ This-> link = mysql_connect ($ dbhost, $ dbuser, $ dbpasswd) or die ($ this-> feedback = 'system Error, Please contect admin ');
@ Mysql_select_db ($ dbtable, $ this-> link) or die ($ this-> feedback = 'system Error, Please contect admin ');
@ Mysql_query ('set names utf8 ');
$ This-> maxline = $ maxline;

// Read the number of rows and return the result $ coutline
$ This-> result = mysql_query ($ query) or die ($ this-> feedback = 'system Error, Please contect admin ');
If ($ count = mysql_fetch_row ($ this-> result )){
// Intval converts a string to an int type, but this type of program is healthier.
$ This-> countline = intval ($ count [0]);
$ This-> countpage = ceil ($ this-> countline/$ maxline );
}
// Determine whether the submitted $ page is greater than the total number of pages
If ($ page <= $ this-> countpage ){
$ This-> page = $ page;
}

If ($ this-> page <2 ){
$ This-> prevpage = 0;
$ This-> nextpage = 2;
$ This-> startline = 0;
} Elseif ($ this-> page ==$ this-> countpage ){
$ This-> prevpage = $ this-> page-1;
$ This-> nextpage = 0;
$ This-> startline = ($ this-> page-1) * $ this-> maxline;
} Else {
$ This-> prevpage = $ this-> page-1;
$ This-> nextpage = $ this-> page + 1;
$ This-> startline = ($ this-> page-1) * $ this-> maxline;
}
}

/*
* Destructor
* Release resources and close database connections
**/
Public function _ destruct (){
Mysql_free_result ($ this-> result );
Mysql_close ($ this-> link );
Exit ();
}

/*
* Output paging information
**/
Public function showpage (){
// $ Listnum: the number of digits in the middle of the upper and lower Pages. it must be an even number! Otherwise, it cannot be divided by 2
$ Listnum = 10;
Echo $ this-> countline. "Items,". $ this-> countpage. "Pages ";

If ($ this-> prevpage = 0 ){
Echo "<Prev ";
} Else {
Echo "prevpage."> <Prev ";
}

If ($ this-> countpage <$ listnum) {// determines whether the total number of pages is less than $ listnum
$ Page_start = 1;
$ Page_end = $ this-> countpage;
} Elseif ($ this-> page <$ listnum/2) {// determines whether the current page is half of $ listnum
$ Page_start = 1;
$ Page_end = $ listnum;
} Elseif ($ this-> page> $ this-> countpage-($ listnum/2) {// determines whether the current page is the last few pages.
$ Page_start = $ this-> countpage-($ listnum-1 );
$ Page_end = $ this-> countpage;
} Else {// if none of the above conditions are met, it is also in the middle
$ Page_start = $ this-> page-($ listnum/2-1 );
$ Page_end = $ this-> page + ($ listnum/2 );
}

For ($ I = $ page_start; $ I <= $ page_end; $ I ++) {// based on the start and end pages judged above, the page number between the cyclic output
If ($ I ==$ this-> page ){
Echo"". $ I ."";
} Else {
Echo "". $ I ."";
}
}

If ($ this-> nextpage = 0 ){
Echo "Next> ";
} Else {
Echo "nextpage."> Next> ";
}

}

/*
* Read data from the database based on SQL statements, and then column it as form output.
* Required variables: $ field, $ table, $ startline, and $ maxline)
* The output starts from the tr of the table and ends with the tr. Therefore, you must add the table label before and after using this method.
**/
Public function showtable ($ query ){
$ Query = $ query. "LIMIT". $ this-> startline. ",". $ this-> maxline;
$ Result = mysql_query ($ query) or die ($ this-> feedback = 'system Error, Please contect admin ');
// Starting from the row loop, define a $ I variable to display the row number. each time a while statement is executed, the pointer points to the next data
$ I = 0;
While ($ fetch = mysql_fetch_assoc ($ result )){
$ I ++;
Echo"". $ I ."";
// The column loop starts. because $ fetch is an array after while, you can use foreach to traverse the array.
Foreach ($ fetch as $ value ){
Echo"". $ Value ."";
}
Echo"";
}
}

/*
* This method is to export resources and customize the style of the table outside.
**/
Public function showresult ($ query ){
$ Result = mysql_query ($ query) or die ($ this-> feedback = 'system Error, Please contect admin ');
Return $ result;
}
}
?>

Variables required for creating an object: $ query (SQL statement used to obtain the number of records from a data table), $ page (current page number), and $ maxline (several rows per page )) * 1. showpage method: If the above object is created...

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.