Paginator code of the php paging program

Source: Internet
Author: User
Tags prev
The code is as follows: Copy code
<? Php
 
Class paginator {
 
Function _ construct (){
 
    }
 
Static function render ($ page_type, array $ args = array ()){
$ Html = '';
Switch ($ page_type)
        {
Case '1 ':
$ Html = page_1 ($ channel_code, $ page_num, $ cur_page, $ per_group, $ base_url, $ n, $ v );
Break;
Case '2 ':
$ Html = fpage ($ row_num, $ pagesize, $ page, $ url );
Break;
Case '3 ':
Break;
 
Default:
$ Html = page ($ num, $ perpage, $ curpage, $ mpurl, $ hiddenFrom = '');
        }
 
Return $ html;
    }
 
 
 
// Function 1: generate paging navigation based on the total page number, current page, page group, and url
Function page_1 ($ channel_code, $ page_num, $ cur_page, $ per_group, $ base_url, $ n, $ v ){
// List of paging function parameters (number of pages, current page, number of pages per page, link address)
$ Base_url. = strpos ($ base_url ,'? ')? '&':'? '; // If Yes ?, Add &; otherwise add?
 
$ Page_val = "";
 
If ($ page_num <1 ){
Return $ page_val; // if the value is smaller than 1, null is returned.
} Elseif ($ page_num = 1 ){
Return $ page_val = 1; // if the value is 1, 1 is returned.
        }
 
// Group
 
$ Group_ OK =! ($ Page_num-1) % ($ per_group-1); // no remainder
// Calculate the total number of groups
If ($ group_ OK ){
$ Group_num = ($ page_num-1)/($ per_group-1 );
} Else {
$ Group_num = ceil ($ page_num-1)/($ per_group-1 ));
} // Determine whether the current page is the last one in a group
 
 
// Number of groups
$ Is_last_page =! ($ Cur_page-1) % ($ per_group-1); // calculates the number of groups currently in
If ($ is_last_page ){
$ Cur_group = ($ cur_page-1)/($ per_group-1) + 1;
} Else {
$ Cur_group = ceil ($ cur_page-1)/($ per_group-1 ));
        }
 
// Calculate the start page displayed. This key is
$ From = ($ cur_group-1) * ($ per_group-1) + 1;
$ From = $ cur_group <= 1? 1: $ from;
 
$ To = $ from + $ per_group;
$ To = $ to> $ page_num? $ Page_num + 1: $;
 
For ($ a = $ from; $ a <$ to; $ a ++ ){
If ($! = $ Cur_page ){
 
// Not the current page
$ Page_val. = '<li> <a href = "'. $ base_url. 'Channel = '. $ channel_code. '& n = '. $ n. '& v = '. $ v. '& page = '. $. '"> '. $. '</a> </li> ';
} Else {
// Current page
$ Page_val. = '<li class = "current">'. $ a. '</li> ';
            }
        }
 
// Previous Page Next Page first page Last Page
$ Next = $ cur_page + 1; // The current page + 1
$ Prev = $ cur_page-1; // Current page-1
 
If ($ cur_page! = $ Page_num ){
// The current page is not the last page
$ Page_val. = '<span> <a href = "'. $ base_url. 'Channel = '. $ channel_code. '& n = '. $ n. '& v = '. $ v. '& page = '. $ next. '"> '. 'Next Page '. '</a> </span> ';
$ Page_val. = '<span> <a href = "'. $ base_url. 'Channel = '. $ channel_code. '& n = '. $ n. '& v = '. $ v. '& page = '. $ page_num. '"> '. '> '. '</a> </span>'; // The last page.
 
        }
If ($ cur_page! = 1 ){
 
// Not the first page. Add the preceding content
$ Page_val = '<span> <a href = "'. $ base_url. 'Channel = '. $ channel_code. '& n = '. $ n. '& v = '. $ v. '& page = '. $ prev. '"> '. 'previous page '. '</a> </span> '. $ page_val;
$ Page_val = '<span> <a href = "'. $ base_url. 'Channel = '. $ channel_code. '& n = '. $ n. '& v = '. $ v. '& page = 1 "> '. '<'. '</a> </span> '. $ page_val; // home page
 
        }
$ Page_val = '<ul class = "page_nav">'. $ page_val. '</ul> ';
 
 
Return $ page_val;
    }
 
 
 
 
// Function 2: based on the number of records, the number of page column cleanup, the current page, and the url year by page
Function fpage ($ row_num, $ pagesize, $ page, $ url ){
// $ Page, current page; $ row_num record count; $ pagesize: number of records per page; $ url record page;
// If there is a number, add 1
$ Page_count = $ row_num % $ pagesize? Intval ($ row_num/$ pagesize) + 1: intval ($ row_num/$ pagesize );
 
If ($ page_count = 1 ){
$ Page_string = "first page | previous page | next page | last page ";
} Else {
// If there are more than one page
If ($ page = 1 ){
// The first page is displayed.
$ Page_string = "<a href =". $ url ."? Page = 1> first page </a> | previous page | <a href = ". $ url ."? Page = ". ($ page + 1)."> Next page </a> | <a href = ". $ url ."? Page = ". $ page_count."> Last page </a> ";
            }
If ($ page> 1) and ($ page <$ page_count )){
$ Page_string = "<a href =". $ url ."? Page = 1> page 1 </a> | <a href = ". $ url ."? Page = ". ($ page-1)."> Previous page </a> | <a href = ". $ url ."? Page = ".
($ Page + 1). "> Next page </a> | <a href =". $ url ."? Page = ". $ page_count."> Last page </a> ";
            }
If ($ page = $ page_count ){
$ Page_string = "<a href =". $ url ."? Page = 1> page 1 </a> | <a href = ". $ url ."? Page = ". ($ page-1)."> Previous page </a> | next page | <a href = ". $ url ."? Page = ". $ page_count."> Last page </a> ";
            }
        }
$ Page_str = $ page_string. (",". $ page. "/". $ page_count. "page ");
Echo "<br> <p align = center>". $ page_str. "</p> ";
    }
 
 
/**
* Paging functions
 *
* @ Param int $ num: total number of records
* @ Param int $ perpage: records on each page
* @ Param int $ curpage: current page
* @ Param string $ mpurl: path url
* @ Param unknown_type $ hiddenFrom
* @ Return string
*/
Function page ($ num, $ perpage, $ curpage, $ mpurl, $ hiddenFrom = ''){
$ Multipage = '';
// Restore
$ Mpurl. = strpos ($ mpurl ,'? ')? '&':'? ';
 
If ($ num> $ perpage ){
$ Page = 10;
$ Offset = 2;
 
$ Pages = @ ceil ($ num/$ perpage); // total number of pages
 
If ($ page> $ pages ){
$ From = 1;
$ To = $ pages;
} Else {
// If the total number of pages is greater than 10
$ From = $ curpage-$ offset; // Current page-2
$ To = $ from + $ page-1; // Current page + 10-3
 
If ($ from <1 ){
 
$ To = $ curpage + 1-$ from;
$ From = 1;
If ($ to-$ from <$ page ){
$ To = $ page;
                    }
} Elseif ($ to> $ pages ){
$ From = $ pages-$ page + 1;
$ To = $ pages;
                }
            }
 
If ($ hiddenFrom ){
$ Multipage = ($ curpage-$ offset> 1 & $ pages> $ page? '<A href = "#"'. js_page ($ hiddenFrom, 1). '> homepage </a> ':'').
($ Curpage> 1? '<A href = "#"'. js_page ($ hiddenFrom, $ curpage-1). '> publish previous page </a> ':'');
For ($ I = $ from; $ I <= $ to; $ I ++ ){
$ Multipage. = $ I ==$ curpage? '<Span>'. $ I. '</span> ':
'<A href = "#"'. js_page ($ hiddenFrom, $ I). '>'. $ I. '</a> ';
                }
 
$ Multipage. = ($ curpage <$ pages? '<A href = "#"'. js_page ($ hiddenFrom, $ curpage + 1). '> Next Page › </a> ':'').
($ To <$ pages? '<A href = "#"'. js_page ($ hiddenFrom, $ pages). '> last page </a> ':'');
$ Multipage = $ multipage? '<Div class = "list-page">'. $ multipage. '</div> ':'';
} Else {
 
$ Multipage = ($ curpage-$ offset> 1 & $ pages> $ page? '<A href = "'. $ mpurl. 'page = 1"> homepage </a> ':'').
($ Curpage> 1? '<A href = "'. $ mpurl. 'Page = '. ($ curpage-1).'"> publish previous page </a> ':'');
 
For ($ I = $ from; $ I <= $ to; $ I ++ ){
$ Multipage. = $ I ==$ curpage? '<Span>'. $ I. '</span> ':
'<A href = "'. $ mpurl. 'Page = '. $ I.'"> '. $ I.' </a> ';
                }
 
$ Multipage. = ($ curpage <$ pages? '<A href = "'. $ mpurl. 'Page = '. ($ curpage + 1).'"> Next page › </a> ':'').
($ To <$ pages? '<A href = "'. $ mpurl. 'Page = '. $ pages.'"> Last page </a> ':'');
$ Multipage = $ multipage? '<Div class = "list-page">'. $ multipage. '</div> ':'';
            }
        }
Return $ multipage;
    }
// No
Function page_get_start ($ page, $ ppp, $ totalnum ){
$ Totalpage = ceil ($ totalnum/$ ppp );
$ Page = max (1, min ($ totalpage, intval ($ page )));
Return ($ page-1) * $ ppp;
    }
// No
Function js_page ($ hiddenFrom, $ page)
    {
If ($ hiddenFrom ){
Return 'onclick = javascript: $ ("# page "). val ("'. $ page. '"); $ ("#'. $ hiddenFrom. '"). submit ();';
        }
    }
}
 
 
 
?>

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.