One instance php mysql template paging class

Source: Internet
Author: User
Tags chr php mysql php tutorial prev
<? Php Tutorial
/*
* The template paging class is derived from the paging method of the database tutorial in the easp tutorial. It is an independent php version of the easp paging.
* Supports dynamic and static paging.
** Page ([total number of records = 1], [page size = 20], [current page = 1], [display page size = 6], [paging parameter = 'page'], [paging link = current page], [static = false])
* Dynamic:
* Simple usage:
* $ Page = new page (50 );
* $ Page-> setpager ('zjj ',' <div class = "newpager"> Total {recordcount} items & nbsp; current & nbsp; {pageindex} & nbsp; page & nbsp;/& nbsp; total & nbsp; {pagecount} & nbsp; page & nbsp;: & nbsp; {first} {prev} & nbsp; {list} & nbsp; {next} {last} & nbsp; go to & nbsp; {jump} & nbsp; page </div> ', array ("listlong" => "6", "first" => "homepage ", "last" => "last page", "prev" => "previous page", "next" => "next page", "list" => "page ", "jump" => "select "));
* Echo $ page-> getpager ('zjj ');
* Full parameter usage:
* $ Page = new page (50, 20, 'page', 'prrr. Php', false );
* $ Page-> setpager ('zjj ',' <div class = "newpager"> Total {recordcount} items & nbsp; current & nbsp; {pageindex} & nbsp; page & nbsp;/& nbsp; total & nbsp; {pagecount} & nbsp; page & nbsp;: & nbsp; {first} {prev} & nbsp; {list} & nbsp; {next} {last} & nbsp; go to & nbsp; {jump} & nbsp; page </div> ', array ("listlong" => "6", "first" => "homepage ", "last" => "last page", "prev" => "previous page", "next" => "next page", "list" => "page ", "jump" => "select "));
* Echo $ page-> getpager ('zjj ');
* Static:
* $ Page = new page(50,20,1,6, 'page', 'prrr?page=.html ', true );
* $ Page-> setpager ('zjj ',' <div class = "newpager"> Total {recordcount} items & nbsp; current & nbsp; {pageindex} & nbsp; page & nbsp;/& nbsp; total & nbsp; {pagecount} & nbsp; page & nbsp;: & nbsp; {first} {prev} & nbsp; {list} & nbsp; {next} {last} & nbsp; go to & nbsp; {jump} & nbsp; page </div> ', array ("listlong" => "6", "first" => "homepage ", "last" => "last page", "prev" => "previous page", "next" => "next page", "list" => "page ", "jump" => "select "));
* Echo $ page-> getpager ('zjj ');
*/
Class page {
Private $ page_size; // Number of Entries displayed on each page
Private $ total_size; // total number of items
Private $ current_page; // the currently selected page
Private $ sub_pages; // The number of pages displayed each time
Private $ total_pages; // total number of pages
Private $ page_tpl = array (); // pagination Template
Private $ pageparam;
Private $ pagelink;
Private $ static;
 
Function _ construct ($ total_size = 1, $ page_size = 20, $ current_page = 1, $ sub_pages = 6, $ pageparam = 'page', $ pagelink = '', $ static = false ){
$ This-> page_size = intval ($ page_size );
$ This-> total_size = intval ($ total_size );
If (! $ Current_page ){
$ This-> current_page = 1;
} Else {
$ This-> current_page = intval ($ current_page );
  }
$ This-> total_pages = ceil ($ total_size/$ page_size );
$ This-> sub_pages = intval ($ sub_pages );
$ This-> pageparam = $ pageparam;
$ This-> pagelink = (empty ($ pagelink )? $ _ Server ["php_self"]: $ pagelink );
$ This-> static = $ static;
$ This-> page_tpl ['default'] = array ('tpl' => '<div class = "pager"> {first} {prev} {liststart} {list }{ listend} {next} {last} jumps to the {jump} page </div> ', 'config' => array ());
 
 }
Public function _ set ($ param, $ value ){
$ This-> $ param = $ value;
 }
Public function _ get ($ param ){
Return $ this-> $ param;
 }
/*
_ Destruct: a destructor called when the class is not in use. This function is used to release resources.
*/
Function _ destruct (){
Unset ($ page_size); // Number of Entries displayed on each page
Unset ($ total_size); // total number of items
Unset ($ current_page); // the currently selected page
Unset ($ sub_pages); // Number of pages displayed each time
Unset ($ total_pages); // total number of pages
Unset ($ page_tpl); // page template
Unset ($ pageparam); // page parameters, default page
Unset ($ pagelink );
Unset ($ static );
 }
Private function urlparameters ($ url = array ()){
Foreach ($ url as $ key => $ val ){
If ($ key! = $ This-> pageparam)
$ Arg [] = $ key. '='. $ val;
  }
$ Arg [] = $ this-> pageparam. '= *';
If ($ this-> static)
Return str_replace ('{page}', '*', $ this-> pagelink );
Else
Return $ this-> pagelink .'? '. Implode (' & ', $ arg );
 }
Public function setpager ($ tpl_name = 'default', $ tpl = '', $ config = array ()){
If (empty ($ tpl ))
$ Tpl = $ this-> page_tpl ['default'] ['tpl'];
If (empty ($ config ))
$ Config = $ this-> page_tpl ['default'] ['config'];
$ This-> page_tpl [$ tpl_name] = array ('tpl' => $ tpl, 'config' => $ config );
 }
Public function getpager ($ tpl_name = 'default '){
$ This-> getcurrentpage ();
Return $ this-> pager ($ this-> page_tpl [$ tpl_name]);
 }
Public function getcurrentpage (){
$ This-> current_page = ($ _ get [$ this-> pageparam] <= intval ($ this-> total_pages )? ($ _ Get [$ this-> pageparam] <1? 1: $ _ get [$ this-> pageparam]): intval ($ this-> total_pages ));
 }
Public function pager ($ page_tpl = ''){
If (empty ($ page_tpl ))
$ Page_tpl = $ this-> page_tpl ['default'];
$ Cfg = array ('recordcount' => intval ($ this-> total_size), 'pageindex' => intval ($ this-> current_page ), 'pagecount' => intval ($ this-> total_pages), 'pagesize' => intval ($ this-> page_size ), 'listlong' => intval ($ this-> sub_pages), 'listsidelong '=> 2, 'list' =>' * ', 'currentclass' => 'current ', 'link' => $ this-> urlparameters ($ _ get), 'first' => '& laquo;', 'prev' => '& #8249 ;', 'next' => '& #8250;', 'last' => '& raquo;', 'more' => '... ', 'disabledclass' => 'disabled', 'jup' => 'input', 'jumpplus '=> '', 'jumpaction' => '', 'jumplong '=> 50 );
If (! Empty ($ page_tpl ['config']) {
Foreach ($ page_tpl ['config'] as $ key => $ val ){
If (array_key_exists ($ key, $ cfg ))
$ Cfg [$ key] = $ val;
   }
  }
$ Tmps tutorial tr = $ page_tpl ['tpl'];
$ Pstart = $ cfg ['pageindex']-($ cfg ['listlong']/2) + ($ cfg ['listlong'] % 2) + 1;
$ Pend = $ cfg ['pageindex'] + $ cfg ['listlong']/2;
If ($ pstart <1 ){
$ Pstart = 1;
$ Pend = $ cfg ['listlong'];
  }
If ($ pend> $ cfg ['pagecount']) {
$ Pstart = $ cfg ['pagecount']-$ cfg ['listlong'] + 1;
$ Pend = $ cfg ['pagecount'];
  }
If ($ pstart <1)
$ Pstart = 1;
For ($ I = $ pstart; $ I <= $ pend; $ I ++ ){
If ($ I = $ cfg ['pageindex'])
$ Plist. = '<span class = "'. $ cfg ['currentclass']. '"> '. str_replace ('*', $ I, $ cfg ['list']). '</span> ';
Else
$ Plist. = '<a href = "'. str_replace ('*', $ I, $ cfg ['link']). '"> '. str_replace ('*', $ I, $ cfg ['list']). '</a> ';
  }
If ($ cfg ['listsidelong ']> 0 ){
If ($ cfg ['listsidelong'] <$ pstart ){
For ($ I = 1; $ I <= $ cfg ['listsidelong ']; $ I ++ ){
$ Pliststart. = '<a href = "'. str_replace ('*', $ I, $ cfg ['link']). '"> '. str_replace ('*', $ I, $ cfg ['list']). '</a> ';
    }
$ Pliststart. = ($ cfg ['listsidelong '] + 1) = $ pstart? '': $ Cfg ['more'].'';
} Else {
If ($ cfg ['listsidelong '] >=$ pstart & $ pstart> 1 ){
For ($ I = 1; $ I <= ($ pstart-1); $ I ++ ){
$ Pliststart. = '<a href = "'. str_replace ('*', $ I, $ cfg ['link']). '"> '. str_replace ('*', $ I, $ cfg ['list']). '</a> ';
     }
    }
   }
If ($ cfg ['pagecount']-$ cfg ['listsidelong '])> $ pend ){
$ Plistend = ''. $ cfg ['more']. $ plistend;
For ($ I = ($ cfg ['pagecount']-$ cfg ['listsidelong ']) + 1); $ I <= $ cfg ['pagecount']; $ I ++ ){
$ Plistend. = '<a href = "'. str_replace ('*', $ I, $ cfg ['link']). '"> '. str_replace ('*', $ I, $ cfg ['list']). '</a> ';
    }
} Else {
If ($ cfg ['pagecount']-$ cfg ['listsidelong ']) <= $ pend & $ pend <$ cfg ['pagecount']) {
For ($ I = ($ pend + 1); $ I <= $ cfg ['pagecount']; $ I ++ ){
$ Plistend. = '<a href = "'. str_replace ('*', $ I, $ cfg ['link']). '"> '. str_replace ('*', $ I, $ cfg ['list']). '</a> ';
     }
    }
   }
  }
If ($ cfg ['pageindex']> 1 ){
$ Pfirst = '<a href = "'. str_replace ('*', '1', $ cfg ['link']). '"> '. $ cfg ['first']. '</a> ';
$ Pprev = '<a href = "'. str_replace ('*', $ cfg ['pageindex']-1, $ cfg ['link']). '"> '. $ cfg ['prev']. '</a> ';
} Else {
$ Pfirst = '<span class = "'. $ cfg ['disabledclass']. '">'. $ cfg ['first']. '</span> ';
$ Pprev = '<span class = "'. $ cfg ['disabledclass']. '">'. $ cfg ['prev']. '</span> ';
  }
If ($ cfg ['pageindex'] <$ cfg ['pagecount']) {
$ Plast = '<a href = "'. str_replace ('*', $ cfg ['pagecount'], $ cfg ['link']). '"> '. $ cfg ['last']. '</a> ';
$ Pnext = '<a href = "'. str_replace ('*', $ cfg ['pageindex'] + 1, $ cfg ['link']). '"> '. $ cfg ['Next']. '</a> ';
} Else {
$ Plast = '<span class = "'. $ cfg ['disabledclass']. '">'. $ cfg ['last']. '</span> ';
$ Pnext = '<span class = "'. $ cfg ['disabledclass']. '">'. $ cfg ['Next']. '</span> ';
  }
Switch (strtolower ($ cfg ['Jump ']) {
Case 'input ':
$ Pjumpvalue = 'This. Value ';
$ Pjump = '<input type = "text" size = "3" title = "enter the number of pages to jump to and press enter "'. ($ cfg ['jumpplus '] = '')? '':''. $ Cfg ['jumpplus ']);
$ Pjump. = 'onkeydown = "webpage effect: if (event. charcode = 13 | event. keycode = 13) {if (! Isnan ('. $ pjumpvalue .')){';
$ Pjump. = ($ cfg ['jumpaction'] = ''? (Strtolower (substr ($ cfg ['link'], 0, 11) = 'javascript :')? Str_replace ('*', $ pjumpvalue, substr ($ cfg ['link'], 12): "document. location. href = '". str_replace ('*', '+ '. $ pjumpvalue. '+ '', $ cfg ['link']). ''; '): str_replace (" * ", $ pjumpvalue, $ cfg ['jumpaction']);
$ Pjump. = '} return false;} "/> ';
Break;
Case 'select ':
$ Pjumpvalue = "this. options [this. selectedindex]. value ";
$ Pjump = '<select'. ($ cfg ['jumpplus '] = ''? ''. $ Cfg ['jumpplus ']. 'Onchange =" javascript:': $ cfg ['jumpplus ']);
$ Pjump. = ($ cfg ['jumpaction'] = ''? (Strtolower (substr ($ cfg ['link'], 0, 11) = 'javascript :')? Str_replace ('*', $ pjumpvalue, substr ($ cfg ['link'], 12): "document. location. href = '". str_replace ('*', '+ '. $ pjumpvalue. '+ '', $ cfg ['link']). ''; '): str_replace (" * ", $ pjumpvalue, $ cfg ['jumpaction']);
$ Pjump. = '"title =" select the number of pages to jump to "> ';
If ($ cfg ['jumplong '] = 0 ){
For ($ I = 0; $ I <= $ cfg ['pagecount']; $ I ++ ){
$ Pjump. = '<option value = "'. $ I. '"'. ($ I = $ cfg ['pageindex'])? 'Selected = "selected" ': '').'> '. $ I.' </option> ';
     }
} Else {
$ Pjumplong = intval ($ cfg ['jumplong ']/2 );
$ Pjumpstart = ($ cfg ['pageindex']-$ pjumplong) <1 )? 1: ($ cfg ['pageindex']-$ pjumplong ));
$ Pjumpstart = ($ cfg ['pagecount']-$ cfg ['pageindex']) <$ pjumplong )? ($ Pjumpstart-($ pjumplong-($ cfg ['pagecount']-$ cfg ['pageindex']) + 1): $ pjumpstart );
$ Pjumpstart = ($ pjumpstart <1 )? 1: $ pjumpstart );
$ J = 1;
For ($ I = $ pjumpstart; $ I <= $ cfg ['pageindex']; $ I ++, $ j ++ ){
$ Pjump. = '<option value = "'. $ I. '"'. ($ I = $ cfg ['pageindex'])? 'Selected = "selected" ': '').'> '. $ I.' </option> ';
     }
$ Pjumplong = $ cfg ['pagecount']-$ cfg ['pageindex'] <$ pjumplong? $ Pjumplong: $ pjumplong + ($ pjumplong-$ j) + 1;
$ Pjumpend = $ cfg ['pageindex'] + $ pjumplong> $ cfg ['pagecount']? $ Cfg ['pagecount']: $ cfg ['pageindex'] + $ pjumplong;
For ($ I = $ cfg ['pageindex'] + 1; $ I <= $ pjumpend; $ I ++ ){
$ Pjump. = '<option value = "'. $ I. '">'. $ I. '</option> ';
     }
    }
$ Pjump. = '</select> ';
Break;
  }
$ Patterns = array ('/{recordcount}/', '/{pagecount}/', '/{pageindex}/', '/{pagesize }/', '/{list}/', '/{liststart}/', '/{listend}/', '/{first}/', '/{prev }/', '/{next}/', '/{last}/', '/{jump }/');
$ Replace = array ($ cfg ['recordcount'], $ cfg ['pagecount'], $ cfg ['pageindex'], $ cfg ['pagesize'], $ plist, $ pliststart, $ plistend, $ pfirst, $ pprev, $ pnext, $ plast, $ pjump );
$ Tmpstr = chr (13). chr (10). preg_replace ($ patterns, $ replace, $ tmpstr). chr (13). chr (10 );
Unset ($ cfg );
Return $ tmpstr;
 }
}
?>

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.