MySQL + PhP paging class (tested)

Source: Internet
Author: User

CopyCode The Code is as follows: <? PHP
/*
Mysql_pager.class.php

Three parameters. Mysql_query () result, URL variable page, the number of records per page you want
The example is at the bottom of the file.
Danshui Riverside Sorting Test
*/

Class mysql_pager {
// Define Properties
VaR $ page;
VaR $ result;
VaR $ results_per_page = 3;
VaR $ total_pages;

/*
Define the methods

The following is a constructor with the same name as the class (> PhP4)
Result handle to be queried, current page number, number of records per page
Like: $ F-> mysql_pager ($ result, 1, 15 );
*/

Function mysql_pager ($ result, $ current_page, $ results_per_page ){

If (! $ Result ){
Echo "<Div align = center> the database is not running, and the result set is incorrect </div> \ n ";
Return;
}

$ This-> result = $ result;

If (! $ Current_page | $ current_page <0)
$ This-> page = 1;
Else $ this-> page = $ current_page;

If (! Emptyempty ($ results_per_page ))
$ This-> results_per_page = $ results_per_page;

$ Numrows = @ mysql_num_rows ($ this-> result );
If (! $ Numrows ){
Echo "<Div align = center> the query result is empty. </div> \ n ";
Return;
}

$ This-> total_pages = Ceil ($ numrows/$ this-> results_per_page );
}

/*
The following is a function for printing content, which can be used or expanded as needed.
The ID is printed here.
*/

Function print_paged_results (){
Echo "<Table border = 0 align = center> \ n ";
$ Start = ($ this-> page-1) * $ this-> results_per_page;
Mysql_data_seek ($ this-> result, $ start );
$ X = 0;
For ($ I = 1; $ I <= $ this-> results_per_page & $ ROW = @ mysql_fetch_array ($ this-> result); $ I ++ ){
If ($ X ++ & 1) $ bgcolor = "# f2f2ff ";
Else $ bgcolor = "# eeeeee ";

Echo "<tr bgcolor = $ bgcolor> <TD>". $ row ["ID"]. "</TD> </tr> ";
// Edit this part to output any HTML

}

Echo "</table> \ n ";
}

/*
Below is the function for printing page numbers and links
Call where we need to display the page number
*/

Function print_navigation (){
Global $ php_self;

Echo "<Div align = center> ";

For ($ I = 1; $ I <= $ this-> total_pages; $ I ++) {# loop to print <1 2 3... $ total_pages>
If ($ I = 1 & $ this-> page> 1) # prints the <first to goto the previous page (not on page 1)
Echo "<a href = \" $ php_self? Page = ". ($ this-> page-1 ). "\" onmouseover = \ "status =" Previous Page "; return true; \" onmouseout = \ "status =" "; return true; \">? </A> ";

If ($ I = $ this-> page) # doesn "t print a link itself, just prints page number
Echo "<font color = \" # ff3333 \ "> $ I </font> ";

If ($ I! = $ This-> page) # other links that aren "t this page go here
Echo "<a href = \" $ php_self? Page = $ I \ "onmouseover = \" status = "Go to page $ I"; return true; \ "onmouseout = \" status = ""; return true; \ "> $ I </a> ";

If ($ I ==$ this-> total_pages & $ this-> page! = $ This-> total_pages) # Link for next page> (not on last page)
Echo "<a href = \" $ php_self? Page = ". ($ this-> page + 1 ). "\" onmouseover = \ "status =" go to the next page "; return true; \" onmouseout = \ "status =" "; return true; \">? </A> ";
}

Echo "</div> \ n ";
}

}

/*
Mysql_connect ($ server, $ uname, $ pass );
Mysql_select_db ("$ DB ");
$ Result = @ mysql_query ("select * from table ");

$ P = new mysql_pager ($ result, $ page = $ _ Get ["page"], 10 );
$ P-> print_navigation ();
$ P-> print_paged_results ();
$ P-> print_navigation ();
*/
?>

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.