Phpmysql paging class (getting started with php)
-
/***
- * Php mysql paging class
- * Organize http://bbs.it-home.org
- */
- Class Pagination {
- Private $ _ result;
- Private $ _ count; // number of records
- Private $ _ pageMax; // maximum page
- Private $ _ page; // current page
- Private $ _ url;
- Private $ _ startPage; // minimum number of page entries
- Private $ _ endPage; // The end code of the page
- Private $ _ nextPage; // Previous Page
- Private $ _ prePage; // Next page
-
Function _ construct ($ table, $ pageSize, $ getPage ){
- $ This-> _ result = $ GLOBALS ['DB']-> query ('select * from'. $ table );
- $ This-> _ count = $ GLOBALS ['DB']-> getRecordNum ();
- $ This-> _ pageMax = ceil ($ this-> _ count/$ pageSize );
- $ This-> _ result = '';
-
If ($ _ GET [$ getPage] = '')
- $ This-> _ page = 1;
- Else
- $ This-> _ page = max (intval ($ _ GET [$ getPage]), 1 );
- $ This-> _ page = min ($ this-> _ page, $ this-> _ pageMax );
-
$ Offset = ($ this-> _ page-1) * $ pageSize;
- $ SQL = 'select * FROM '. $ table. 'limit'. $ offset.', '. $ pageSize;
- $ This-> _ result = $ GLOBALS ['DB']-> query ($ SQL );
- }
- Function getRecord (){
- Return $ GLOBALS ['DB']-> getRecord ();
- }
- Function getPageBar ($ url = '? ', $ BarLn = 10, $ style = 1 ){
- If ($ style = 1 ){
- If ($ barLn % 2! = 0 ){
- $ Midder = ceil ($ barLn/2 );
- $ Big_repair = $ midder-1; // when an integer is obtained by the preceding method, 1 is subtracted here, and 1 is incremented.
- } Else {
- $ Midder = $ big_repair = $ barLn/2;
- }
- $ Sml_repair = $ midder-1;
- $ This-> _ startPage = ($ this-> _ page + $ midder)> $ this-> _ pageMax? $ This-> _ pageMax-$ barLn: $ this-> _ page-$ sml_repair;
- $ This-> _ endPage = $ this-> _ page <$ midder? $ BarLn: $ this-> _ page + $ big_repair;
-
- } Elseif ($ style = 2 ){
- If ($ this-> _ page % $ barLn = 0 ){
- $ This-> _ startPage = $ this-> _ page;
- } Else {
- $ This-> _ startPage = ($ this-> _ page> $ barLn )? $ This-> _ page-($ this-> _ page % $ barLn): 1;
- }
- $ This-> _ endPage = $ this-> _ startPage + $ barLn-1;
- }
- $ This-> _ url = $ url;
- $ This-> _ nextPage = $ this-> _ page + 1;
- $ This-> _ prePage = $ this-> _ page-1;
- $ This-> _ startPage = max ($ this-> _ startPage, 1); // at least from the first page
- $ This-> _ endPage = min ($ this-> _ endPage, $ this-> _ pageMax); // only the last page can be reached.
- $ This-> _ result = 'current is :'. $ this-> _ page. '/'. $ this-> _ pageMax. 'page, Total '. $ this-> _ count. 'records ';
- If ($ this-> _ page> 1)
- $ This-> _ result. = '_ url.' page = 1 ">
- 9_url. 'page = '. $ this-> _ prePage.' ">
- 3
- ';
- Else
- $ This-> _ result. = '9
- 3 ';
- For ($ I = $ this-> _ startPage; $ I <= $ this-> _ endPage; $ I ++ ){
- If ($ this-> _ page = $ I)
- $ This-> _ result. = ''. $ I .'';
- Else
- $ This-> _ result. = '_ url. 'page ='. $ I. '">'. $ I .'';
- }
- If ($ this-> _ page! = $ This-> _ pageMax ){
- $ This-> _ result. = '_ url.' page = '. $ this-> _ nextPage.' "> 4 ';
- $ This-> _ result. = '_ url. 'page ='. $ this-> _ pageMax. '"> :';
- } Else {
- $ This-> _ result. = '4 :';
- }
- $ This-> _ result. ='
|