A simple and useful PHP paging class _php

Source: Internet
Author: User
Keywords PHP Paging class
Copy CodeThe code is as follows:
Class Page {
Number of pages displayed per page in the page bar
Public $rollPage = 6;
The parameters to take when the page jumps
Public $parameter;
Default list shows the number of rows per page
Public $listRows = 20;
Number of start lines
Public $firstRow;
Number of page total pages
protected $totalPages;
Total number of rows
protected $totalRows;
Current page
protected $nowPage;
Total number of pages in the page bar
protected $coolPages;
Pagination Display Customization
protected $config = Array (
' Redirect ' =>false,
' Header ' = ' record ',
' Prev ' = ' prev ',
' Next ' = ' next page ',
' First ' = ' 1 ',
' Last ' and ' The final page ',
' Theme ' = '%uppage%%first%%prepage%%linkpage%%nextpage%%downpage% Total%totalpage% page ');
Default paging variable name
protected $varPage;

/**
+----------------------------------------------------------
* Schema function
+----------------------------------------------------------
* @access Public
+----------------------------------------------------------
* Total number of records $totalRows @param array
* @param array $listRows shows the number of records per page
* @param the parameters of the array $parameter 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 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;
}
}

/**
+----------------------------------------------------------
* Pagination Display output
+----------------------------------------------------------
* @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 is less than the number of pages displayed
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{
Number of floats
$size = Floor ($this-&GT;ROLLPAGE/2);

$start = $this->nowpage-$size;
$end = $this->nowpage + $size;

$isfirst = true;
$islast = true;
}
}

Page UP and down strings
$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[' header '), $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{
Passing parameters
if ($this->totalpages > 1) {
$redirect = "to the first";
}else{
$redirect = $redirect. '';
}
}
Generating 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.