General PHP pagination class

Page.class.php 
<PHP?
/ **
* page class
*
* invocation:
* the p-$ = new new Page (total number of display pages, current page number, page number of the display, [link]);
* print_r ($ p-> getPages ()) ; // array generates a page number (page number key, a value of the link)
* p-echo $> showPages (. 1); // generates a page style (to add custom styles)
*
* /

/ *
number of the total number of, the number of pages to be displayed, this page, each page is displayed, the connection
to generate a one-dimensional array, the page key is connected to
return a page number generated good pattern (according to their own needs and may Add style)
default styles total 45 Each page 10, the current page 1/4 [first] [Prev] [1] [2] [3] .. [nEXT] [last]
* /
page {class
protected $ cOUNT; // total number of pieces
protected $ showPages; // number of pages you want to display
protected $ countPages; // total number of pages
protected $ currPage; // current page
protected $ subPages; // item per page the number of
protected $ href; // connection
protected $ page_arr = array (); // save the generated page is the page key connection

/ **
* constructor the __construct (tab obtain the required parameters)
* @param $ COUNT The total number of int
* @param int $ showPages display page number
* @param int $ currPage pages
* @param int $ subPages page shows the number
* @param string $ href connection (do not get the current setting is the URL of)
* /
public function __construct ($ cOUNT, $ showPages, $ currPage, subPages $, $ the href = '') {
$ this-> $ COUNT = COUNT;
$ this-> = $ showPages showPages;
$ this-> = $ currPage currPage;
$ this-> = $ subPages subPages;

// if the link not set is acquired current connection
IF (empty (the href $)) {
$ this-> the href = the htmlentities ($ _ SERVER [ 'PHP_SELF']);
} the else {
$ this-> the href the href = $;
}
$ this-> construct_Pages ();
}

/**
* GetPages Returns an array of p
* @return array of one-dimensional array value of the page on the link key
* /
public GetPages function () {
return $ this-> page_arr;
}

/ **
* returns the resulting good showPages p
* @param int $ style style
* @return string generating good page
* /
public function showPages ($ style =. 1) {
$ FUNC = 'pagestyle' $ style;.
return the this $ -> $ FUNC ();
}

/ **
* pageStyle1 style tab ( this may refer, for example, to add custom styles pageStyle2 ())
* total 45 style Each page 10, the current page 1/4 [first] [Prev] [1] [2] [3] .. [ next] [last]
* @return String
* /
protected pageStyle1 function () {
/ * normal mode tab configured
total 4523 Each page 10, page 1/453 current [first] [Prev] [1] [2] [3] .. [nEXT] [Last]
* /
$ pageStr = 'total' $ this-> count 'records, per page' $ this-> subPages 'bar';....
$ pageStr = 'Present' $ this-> currPage '/' ... .. $ this-> countPages' page ';

$ _GET [' page '] =. 1;
. $ pageStr =' <span> [<a href="'.$this-> the href. '' http_build_query ($?. _GET) ' "> first </a>] </ span>';.
// if this page is not the first page is displayed on the page
IF ($ this-> currPage>. 1) {
$ _GET [ 'page'] = this- $>-currPage. 1;
. $ pageStr = '<span> [<a href="'.$this-> the href.' "> page </a>? 'http_build_query ($ _ GET )..'] </ span> ';
}

the foreach ($ this-> page_arr AS = K $> $ V) {
$ _GET [' Page '] = K $;
. $ pageStr =' <span> [<A the href = " '... V $ ' ">' $ K '</a>] </ span>';.
}

// If this page is less than the total number of pages is displayed next
if ($ this-> currPage <$ this-> countPages) {
$ _GET [ 'Page'] = $ this-> currPage +. 1;
$pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'">下页</a>] </span>';
}

$_GET['page'] = $this->countPages;
$pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'">尾页</a>] </span>';

return $pageStr;
}

/**
* construct_Pages 生成页码数组
* 键为页码,值为链接
* $this->page_arr=Array(
* [1] => index.php?page=1
* [2] => index.php?page=2
* [3] => index.php?page=3
* ......)
*/
protected function construct_Pages(){
//计算总页数
$this->countPages=ceil($this->count/$this->subPages);$ leftPage_num = floor ($ this->
// this page The front page count

rightPage_num = $ this- $> $ showPages- leftPage_num;

// number of this page on the left shows the display of the left minus the total number of display 7, for example, this page is the minimum 5 left to the right of 5-3. 3 + 5
$ $ left = this-> currPage- $ leftPage_num;
$ left = max ($ left, 1); // can not be less than the minimum left 1
$ + $ left right = $ this-> showPages-1; // add the left to the right of the display page number is decremented It shows the number of
$ right = min ($ right, $ this-> countPages); // can not be greater than the maximum total number of pages on the right
$ left = max ($ right- $ this-> showPages + 1,1); // determine the right and then calculating left, the second to be calculated

for ($ I $ = left; I $ <= $ right; $ I ++) {
$ _GET [ 'Page'] = $ I;
$ this-> page_arr [$ I] = $ this- > http_build_query the href (the GET $ _);. '?'.
}
}
}
>?
demo.php 
<PHP?
/ **
* Demo
* /
header ( "Content-type: text / HTML; charset = UTF8");
the include ( 'Page.class.php');// introduction class

// $ p = new Page (total number of display pages, the current page number, page number of the display, [link]);
// current link was not provided connecting
$ page = isset ($ _ GET [ 'page' ?]) $ _GET [ 'Page']:. 1;
$ Page new new = P (100,4, Page $,. 8);

// array generates a page number (page number key, a value of the link)
echo "<pre>" ;
print_r (p-$> GetPages ());

// style total 45 Each page 10, the current page 1/4 [first] [Prev] [1] [2] [3] .. [next] [Last]
echo $ p-> showPages (. 1);

----------------
Original link: https: //blog.csdn.net/netuser1937/article/details/54863281

Guess you like

Origin www.cnblogs.com/nxmxl/p/11787575.html