Php classic paging code class this is a php paging code is a classic instance code that can automatically determine url parameters and then pagination. At the same time, you have customized the previous page, next page, home page, it also supports the select drop-down box jump code.
Class page {
Var $ page_name = "page ";
Var $ next_page = 'Next page ';
Var $ pre_page = 'previous page ';
Var $ first_page = 'homepage ';
Var $ last_page = 'end page ';
Var $ pre_bar = '<';
Var $ next_bar = '> ';
Var $ format_left = '';
Var $ format_right = '';
Var $ pagebarnum = 5;
Var $ totalpage = 0;
Var $ nowindex = 1;
Var $ url = "";
Var $ offset = 0;
Var $ rewrite = array ();
Function page ($ array)
{
If (is_array ($ array )){
If (! Array_key_exists ('Total', $ array) $ this-> error (_ function __, 'Need a param of total ');
$ Total = intval ($ array ['Total']);
$ Perpage = (array_key_exists ('perpage', $ array ))? Intval ($ array ['perpage']): 10;
$ Nowindex = (array_key_exists ('nowindex ', $ array ))? Intval ($ array ['nowindex ']): '';
$ Url = (array_key_exists ('url', $ array ))? $ Array ['url']: '';
$ Action = (array_key_exists ('action', $ array ))? $ Array ['action']: '';
$ Id0 = (array_key_exists ('id0', $ array ))? $ Array ['id0']: '';
$ Id1 = (array_key_exists ('id1', $ array ))? $ Array ['id1']: '';
$ Id2 = (array_key_exists ('id2', $ array ))? $ Array ['id2']: '';
$ Id3 = (array_key_exists ('id3 ', $ array ))? $ Array ['id3 ']: '';
} Else {
$ Total = $ array;
$ Perpage = 10;
$ Nowindex = '';
$ Url = '';
$ Action = '';
$ Id0 = '';
$ Id1 = '';
$ Id2 = '';
$ Id3 = '';
}
If ((! Is_int ($ total) | ($ total <0) $ this-> error (_ function __, $ total. 'is not a positive integer! ');
If ((! Is_int ($ perpage) | ($ perpage <= 0) $ this-> error (_ function __, $ perpage. 'is not a positive integer! ');
If (! Empty ($ array ['page _ name']) $ this-> set ('page _ name', $ array ['page _ name']);
$ This-> _ set_nowindex ($ nowindex );
$ This-> _ set_url ($ url );
$ This-> totalpage = ceil ($ total/$ perpage );
$ This-> offset = ($ this-> nowindex-1) * $ perpage;
$ This-> action = $ action;
$ This-> rewrite = array ('action' => $ action, 'id0' => $ id0, 'id1' => $ id1, 'id2' => $ id2, 'id3 '=> $ id3 );
}
Function set ($ var, $ value)
{
If (in_array ($ var, get_object_vars ($ this )))
$ This-> $ var = $ value;
Else {
$ This-> error (_ function __, $ var. "does not belong to pb_page! ");
}
}
Function next_page ($ style = ''){
If ($ this-> nowindex <$ this-> totalpage ){
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> nowindex + 1), $ this-> next_page, $ style );
}
Return '<a class = "'. $ style. '">'. $ this-> next_page. '</a> ';
}
Function pre_page ($ style = ''){
If ($ this-> nowindex> 1 ){
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> nowindex-1), $ this-> pre_page, $ style );
}
Return '<a class = "'. $ style. '">'. $ this-> pre_page. '</a> ';
}
Function first_page ($ style = ''){
If ($ this-> nowindex = 1 ){
Return '<a class = "'. $ style. '">'. $ this-> first_page. '</a> ';
}
Return $ this-> _ get_link ($ this-> _ get_url (1), $ this-> first_page, $ style );
}
Function last_page ($ style = ''){
If ($ this-> nowindex = $ this-> totalpage | $ this-> totalpage = 0 ){
Return '<a class = "'. $ style. '">'. $ this-> last_page. '</a> ';
}
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> totalpage), $ this-> last_page, $ style );
}
Function nowbar ($ style = '', $ nowindex_style = '')
{
$ Plus = ceil ($ this-> pagebarnum/2 );
If ($ this-> pagebarnum-$ plus + $ this-> nowindex> $ this-> totalpage) $ plus = ($ this-> pagebarnum-$ this-> totalpage + $ this-> nowindex );
$ Begin = $ this-> nowindex-$ plus + 1;
$ Begin = ($ begin> = 1 )? $ Begin: 1;
$ Return = '';
For ($ I = $ begin; $ I <$ begin + $ this-> pagebarnum; $ I ++)
{
If ($ I <= $ this-> totalpage ){
If ($ I! = $ This-> nowindex)
$ Return. = $ this-> _ get_text ($ this-> _ get_link ($ this-> _ get_url ($ I), $ I, $ style ));
Else
$ Return. = $ this-> _ get_text ('<a class = "'. $ nowindex_style. '">'. $ I. '</a> ');
} Else {
Break;
}
$ Return. = "n ";
}
Unset ($ begin );
Return $ return;
}
/**
* Get the code for displaying the jump button
*
* @ Return string
*/
Function select ()
{
$ Return = '<select name = "pb_page_select"> ';
For ($ I = 1; $ I <= $ this-> totalpage; $ I ++)
{
If ($ I ==$ this-> nowindex ){
$ Return. = '<option value = "'. $ I. '" selected>'. $ I. '</option> ';
} Else {
$ Return. = '<option value = "'. $ I. '">'. $ I. '</option> ';
}
}
Unset ($ I );
$ Return. = '</select> ';
Return $ return;
}
/**
* Obtain the limit value in the mysql tutorial statement.
*
* @ Return string
*/
Function offset ()
{
Return $ this-> offset;
}
/**
* Control the display style by PAGE (you can add the corresponding style)
*
* @ Param int $ mode
* @ Return string
*/
Function show ($ mode = 1)
{
Switch ($ mode)
{
Case '1 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
Return $ this-> pre_page (). $ this-> nowbar (). $ this-> next_page (). 'di '. $ this-> select (). 'page ';
Break;
Case '2 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
$ This-> first_page = 'homepage ';
$ This-> last_page = 'last page ';
Return $ this-> first_page (). $ this-> pre_page (). '[nth '. $ this-> nowindex. 'page] '. $ this-> next_page (). $ this-> last_page (). 'di '. $ this-> select (). 'page ';
Break;
Case '3 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
$ This-> first_page = 'homepage ';
$ This-> last_page = 'last page ';
Return $ this-> first_page ("page_box "). "". $ this-> pre_page ("page_box "). "". $ this-> nowbar ("page_box_a", "page_box_ B "). "". $ this-> next_page ("page_box "). "". $ this-> last_page ("page_box "). "<a class =" clear "> </a> ";
Break;
Case '4 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
Return $ this-> pre_page (). $ this-> nowbar (). $ this-> next_page ();
Break;
Case '5 ':
Return $ this-> pre_bar (). $ this-> pre_page (). $ this-> nowbar (). $ this-> next_page (). $ this-> next_bar ();
Break;
}
}
Function _ set_url ($ url = "")
{
If (! Empty ($ url )){
$ This-> url = $ url. (stristr ($ url ,'? '))? '&':'? '). $ This-> page_name. "= ";
} Else {
If (empty ($ _ server ['query _ string']) {
$ This-> url = $ _ server ['request _ uri ']. "? ". $ This-> page_name." = ";
} Else {
If (stristr ($ _ server ['query _ string'], $ this-> page_name. '= ')){
$ This-> url = str_replace ($ this-> page_name. '='. $ this-> nowindex, '', $ _ server ['request _ uri ']);
$ Last = $ this-> url [strlen ($ this-> url)-1];
If ($ last = '? '| $ Last = '&'){
$ This-> url. = $ this-> page_name. "= ";
} Else {
$ This-> url. = '&'. $ this-> page_name. "= ";
}
} Else {
$ This-> url = $ _ server ['request _ uri '].' & '. $ this-> page_name.' = ';
}
}
}
}
Function _ set_nowindex ($ nowindex)
{
If (empty ($ nowindex )){
If (isset ($ _ get [$ this-> page_name]) {
$ This-> nowindex = intval ($ _ get [$ this-> page_name]);
}
} Else {
$ This-> nowindex = intval ($ nowindex );
}
}
Function _ get_url ($ pageno = 1)
{
Global $ _ cfg;
$ Arr = $ this-> rewrite;
// Print_r ($ arr );
// Print_r ($ this-> url. $ pageno );
If ($ _ cfg ['urlrewrite'] &! Empty ($ arr ['action']) {
Return url_rewrite ($ arr ['action'], array ('id0' => $ arr ['id0'], 'id1' => $ arr ['id1'], 'id2' => $ arr ['id2'], 'id3 '=> $ arr ['id3'], 'page' => $ pageno ));
} Else {
Return $ this-> url. $ pageno;
}
}
Function _ get_text ($ str)
{
Return $ this-> format_left. $ str. $ this-> format_right;
}
Function _ get_link ($ url, $ text, $ style = ''){
$ Style = (empty ($ style ))? '': 'Class =" '. $ style .'"';
Return '<a'. $ style. 'href = "'. $ url.'"> '. $ text.' </a> ';
}
Function error ($ function, $ errormsg)
{
Die ('error in file <B> '. _ file __. '</B>, function <B> '. $ function. '() </B> :'. $ errormsg );
}
}
?>