A simple and useful php paging class

Source: Internet
Author: User
A simple and useful php paging class. For more information, see.

A simple and useful php paging class. For more information, see.

The Code is as follows:


Class Page {
// Number of pages displayed on each page in the pagination bar
Public $ rollPage = 6;
// Parameters to be included during page redirect
Public $ parameter;
// The number of lines per page in the default list
Public $ listRows = 20;
// Start row
Public $ firstRow;
// Total number of pages
Protected $ totalPages;
// Total number of rows
Protected $ totalRows;
// Current page number
Protected $ nowPage;
// The total number of pages in the paging Column
Protected $ coolPages;
// Custom display by PAGE
Protected $ config = array (
'Redirect' => false,
'Header' => 'record ',
'Prev' => 'previous page ',
'Next' => 'Next page ',
'First' => '1 ',
'Last' => 'last page ',
'Theme '=>'

% UpPage % first % prePage % linkPage % nextPage % downPage %

% TotalPage % page' in total ');
// Default paging variable name
Protected $ varPage;

/**
+ ----------------------------------------------------------
* Architecture functions
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param array $ total number of totalRows records
* @ Param array $ number of records displayed on each page of listRows
* @ Param array $ parameters for paging jump
+ ----------------------------------------------------------
*/
Public function _ construct ($ totalRows, $ listRows = '', $ parameter = ''){
$ This-> totalRows = $ totalRows;
$ This-> parameter = $ parameter;
$ This-> varPage = C ('var _ page ')? C ('var _ page'): 'P ';
If (! Empty ($ listRows )){
$ This-> listRows = intval ($ listRows );
}
$ This-> totalPages = ceil ($ this-> totalRows/$ this-> listRows); // total number of pages
$ This-> coolPages = ceil ($ this-> totalPages/$ this-> rollPage );
// $ _ GET verification
$ This-> nowPage = intval ($ _ GET [$ this-> varPage]);
$ This-> nowPage = $ this-> nowPage> 0? $ This-> nowPage: 1;

If (! Empty ($ this-> totalPages) & $ this-> nowPage> $ this-> totalPages ){
$ This-> nowPage = $ this-> totalPages;
}
$ This-> firstRow = $ this-> listRows * ($ this-> nowPage-1 );
}

Public function setConfig ($ name, $ value ){
If (isset ($ this-> config [$ name]) {
$ This-> config [$ name] = $ value;
}
}

/**
+ ----------------------------------------------------------
* Display output by PAGE
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
*/
Public function show (){

If (0 = $ this-> totalRows) return '';

// Processing Parameters
$ P = $ this-> varPage;
$ Url = $ _ SERVER ['request _ URI ']. (strpos ($ _ SERVER ['request _ URI'], '? ')? '':"? "). $ This-> parameter;
$ Parse = parse_url ($ url );
If (isset ($ parse ['query']) {
Parse_str ($ parse ['query'], $ params );
Unset ($ params [$ p]);
$ Url = $ parse ['path']. '? '. Http_build_query ($ params );
}

/* Paging logic */

// When the total number is smaller than the displayed page number
If ($ this-> totalPages <= $ this-> rollPage ){
$ Start = 1;
$ End = $ this-> totalPages;
}
Else {
//
If ($ this-> nowPage <= $ this-> rollPage-1 ){
$ Start = 1;
$ End = $ this-> rollPage;

$ Islast = true;
}
Else if ($ this-> nowPage> $ this-> totalPages-$ this-> rollPage + 1 ){
$ Start = $ this-> totalPages-($ this-> rollPage-1 );
$ End = $ this-> totalPages;

$ Isfirst = true;
}
Else {
// Floating number
$ Size = floor ($ this-> rollPage/2 );

$ Start = $ this-> nowPage-$ size;
$ End = $ this-> nowPage + $ size;

$ Isfirst = true;
$ Islast = true;
}
}

// Flip string
$ UpRow = $ this-> nowPage-1;
$ DownRow = $ this-> nowPage + 1;


/* Assemble HTML */

// <1 ...... last>
If ($ isfirst ){
$ TheFirst = "". $ this-> config ['first']. "";
}
If ($ islast ){
$ TheEnd = "totalPages '>". $ this-> config ['last']. "";
}

If ($ upRow> 0 ){
$ UpPage = "". $ this-> config ['prev']. "";
}

If ($ downRow <= $ this-> totalPages ){
$ DownPage = "". $ this-> config ['Next']. "";
}

If ($ start = 3 ){
$ LinkPage. = "2 ";
}
If ($ start> = 4 ){
$ LinkPage. = "2 ...";
}
// 1 2 3 4 5
For ($ I = $ start; $ I <= $ end; $ I ++ ){
If ($ I! = $ This-> nowPage ){
$ LinkPage. = "". $ I ."";
} Else {
$ LinkPage. = "". $ I ."";
}
If ($ I = $ end ){
If ($ I <$ this-> totalRows ){
$ LinkPage. = "...";
}
}
}

$ PageStr = str_replace (
Array ('% header %', '% nowPage %', '% totalRow %', '% totalPage %', '% upPage %', '% first % ', '% prePage %', '% linkPage %', '% nextPage %', '% downPage %', '% end % '),
Array ($ this-> config ['head'], $ this-> nowPage, $ this-> totalRows, $ this-> totalPages, $ upPage, $ theFirst, $ prePage, $ linkPage, $ nextPage, $ downPage, $ theEnd), $ this-> config ['Theme ']);

// Display mode normal false with jump ture
If (! Empty ($ this-> config ['redirect']) {
$ Html = $ pageStr;

} Else {
// PASS Parameters
If ($ this-> totalPages> 1 ){
$ Redirect ="

';
} Else {
$ Redirect = $ redirect .'

';
}
}
// Generate an Html string
$ Html = $ pageStr. $ redirect;
}
Return $ html;
}

}

,

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.