逻辑背后的分页就像google [英] logic behind pagination like google
问题描述
google's pagination bahviour背后的逻辑是什么?
我的分页符如下:
[1] 2 3 ... 184>
< 1 [2] 3 4 ... 184>
< 1 2 [3] 4 5 ... 184>
< 1 2 3 [4] 5 6 ... 184>
< 1 ... 3 4 [5] 6 7 ... 184>
< 1 ... 4 5 [6] 7 8 ... 184>
< 1 ... 5 6 [7] 8 9 ... 184>
< 1 ... 6 7 [8] 9 10 ... 184>
以下是上述示例的实时版本:http://www.dev.thomaskile.me/?page=test-zone&module=Paginator 。
我知道为什么会发生这种情况;我已经将当前页面每一侧显示的页面数量设置为两(2)。
我希望数字的范围相等这是:
[1] 2 3 4 5 6 7 8 ... 184>
< 1 [2] 3 4 5 6 7 ... 184>
< 1 2 [3] 4 5 6 7 ... 184>
< 1 2 3 [4] 5 6 7 ... 184>
< 1 ... 3 4 [5] 6 7 ... 184>
< 1 ... 4 5 [6] 7 8 ... 184>
< 1 ... 5 6 [7] 8 9 ... 184>
< 1 ... 6 7 [8] 9 10 ... 184>
这是在开始和结束我需要做出改变,如何使它变得容易操作...
我想使它灵活。意思我想要能够改变每一侧的想要的页面的数量,并让脚本扩展和克隆所有...
这里是我的代码到目前为止:
/ **
*页面控制器按钮
* @ param str $ this-> querySting href =URL string
* @param str $ this-> pageIdentifier $ _GET ['this-name']
* @param int $ this-> numPages页面总数
* @param int $ this-> midRange当前页面每一侧显示的页面数量
* /
public function prevPage()
{
if($ this-> currentPage> 1){
$ prevPage =($ this-> currentPage - 1);
return'< a href ='。$ this-> queryString。'&'。$ this-> pageIdentifier。'='。$ prevPage。'class =prev> ; / a>';
}
}
public function nextPage()
{
if($ this-> currentPage< $ this-> numPages){
$ nextPage = $ this-> currentPage + 1;
return'< a href ='。$ this-> queryString。'&'。$ this-> pageIdentifier。'='。$ nextPage。'class =next> next< ; / a>';
}
}
public function firstPage()
{
if($ this-> currentPage>($ this-> midRange + 1)){ //如果currentPage和firstPage之间的页面数超过$ midRange,则为1 ...
$ firstPage。='< a href ='。$ this-> queryString。 。$ this-> pageIdentifier。'= 1class =first> 1< / a>' // ... showfirst page-link
if($ this-> currentPage>($ this-> midRange + 2)){//如果$ currentPage和first page超过$ midRange且超过1
$ firstPage。='& hellip;'; //在第一页和第一页之间添加...
}
}
return $ firstPage;
}
public function lastPage()
{
if($ this-> currentPage<($ this-> numPages - $ this-> midRange)){ //如果(($ this-> currentPage<($ this-> numPages- $ this-> midRange))如果currentPage和last page之间的页数等于$ midRange
- 1)){//如果$ currentPage和最后一页之间的页面数超过两个以上的$ range
$ lastPage。='& hellip;'; //在最后一页 - 链接和$范围内的最后一页之间添加...
}
$ lastPage。='< a href ='。$ this-> queryString。 '&'。$ this-> pageIdentifier。'='。$ this-> numPages。'class =last>'。$ this-> numPages。'< / a>'; //显示最后一页-link
}
return $ lastPage;
}
#(prev first ...)和(... last next)之间的页面范围
public function listPages()
{
for($ i =($ this-> currentPage- $ this-> midRange); $ i<(($ this-> currentPage + $ this-> midRange)+1); $ i ++){
if(($ i> 0)&&($ i< = $ this-> numPages))//如果页码在页面范围内
{
if ($ i == $ this-> currentPage){$ listPages。='< a class =current>'。$ i。'< / a>'; } //如果我们在当前页面
else {$ listPages。='< a href ='。$ this-> queryString。'&'。$ this-> pageIdentifier。 '。$ i。'>'。$ i。'< / a>'; } //如果不是当前页面
}
}
return $ listPages;
}
我的分页。
$ startPage = $ currentPage - 4;
$ endPage = $ currentPage + 4;
if($ startPage< = 0){
$ endPage - =($ startPage - 1);
$ startPage = 1;
}
if($ endPage> $ totalPage)
$ endPage = $ totalPage;
if($ startPage> 1)echoFirst ...;
for($ i = $ startPage; $ i< = $ endPage; $ i ++)echo{$ i};
if($ endPage< $ totalPage)echo... Last;
我相信我的代码是自我解释,我会尝试用简明的英语解释。首先,您需要知道两件事,然后才能生成分页: $ totalPage 和 $ currentPage 。
第1步:假设当前页面在中档。
:如果 $ startPage ,则会尝试生成页面的$ startPage和$ endPage存储范围。 。第3步:如果 $ endPage 超过 $ totalPage ,则 $ endPage 是最后一页。
步骤4 :将分页生成为HTML。 (这取决于你想要的分页结果,我将简单地使用纯文本代表我的分页)
if ($ startPage> 1)echoFirst ...;
for($ i = $ startPage; $ i< = $ endPage; $ i ++)echo{$ i};
if($ endPage< $ totalPage)echo... Last;
修正了我以前的逻辑
$ startPage =($ curPage< 5)? 1:$ curPage - 4;
$ endPage = 8 + $ startPage;
$ endPage =($ totalPage< $ endPage)? $ totalPage:$ endPage;
$ diff = $ startPage - $ endPage + 8;
$ startPage - =($ startPage - $ diff> 0)? $ diff:0;
if($ startPage> 1)echoFirst ...;
for($ i = $ startPage; $ i< = $ endPage; $ i ++)echo{$ i};
if($ endPage< $ totalPage)echo... Last;
What is the logic behind google's pagination bahviour?
My paginator goes something like this:
[1] 2 3 ... 184 >
< 1 [2] 3 4 ... 184 >
< 1 2 [3] 4 5 ... 184 >
< 1 2 3 [4] 5 6 ... 184 >
< 1 ... 3 4 [5] 6 7 ... 184 >
< 1 ... 4 5 [6] 7 8 ... 184 >
< 1 ... 5 6 [7] 8 9 ... 184 >
< 1 ... 6 7 [8] 9 10 ... 184 >
Here is a live version of the above example: http://www.dev.thomaskile.me/?page=test-zone&module=Paginator.
I know why this is happening; I've set the amount of pagenumbers to be showen on each side of current page to two (2).
I would rather have the range of numbers to be equal like this:
[1] 2 3 4 5 6 7 8 ... 184 >
< 1 [2] 3 4 5 6 7 ... 184 >
< 1 2 [3] 4 5 6 7 ... 184 >
< 1 2 3 [4] 5 6 7 ... 184 >
< 1 ... 3 4 [5] 6 7 ... 184 >
< 1 ... 4 5 [6] 7 8 ... 184 >
< 1 ... 5 6 [7] 8 9 ... 184 >
< 1 ... 6 7 [8] 9 10 ... 184 >
It's at the beginning and the end I need to make som e changes, but can't figure out how to make it an easy operation...
I would like to make it flexible aswell. Meaning I would like to be able to change the number of wanted pages on each side, and have the script expand and claculate it all...
Here is my code so far:
/**
* page controller buttons
* @param str $this->querySting href="URL string"
* @param str $this->pageIdentifier $_GET['this-name']
* @param int $this->numPages Total amount of pages
* @param int $this->midRange Number of pages to show on each side of current page
*/
public function prevPage()
{
if ($this->currentPage > 1){
$prevPage = ($this->currentPage - 1);
return '<a href="'.$this->queryString.'&'.$this->pageIdentifier.'='.$prevPage.'" class="prev">prev</a>';
}
}
public function nextPage()
{
if ($this->currentPage < $this->numPages) {
$nextPage = $this->currentPage + 1;
return '<a href="'.$this->queryString.'&'.$this->pageIdentifier.'='.$nextPage.'" class="next">next</a>';
}
}
public function firstPage()
{
if ($this->currentPage > ($this->midRange + 1)) { // if number of pages between "currentPage" and "firstPage" exceeds $midRange with 1...
$firstPage .= '<a href="'.$this->queryString.'&'.$this->pageIdentifier.'=1" class="first">1</a>'; // ...show "first page"-link
if ($this->currentPage > ($this->midRange + 2)) { // if number of pages between $currentPage and "first page" exceeds $midRange with more than 1
$firstPage .= '…'; // add "..." between "1st page"-link and first page in $range
}
}
return $firstPage;
}
public function lastPage()
{
if ($this->currentPage < ($this->numPages - $this->midRange)) { // if number of pages between "currentPage" and "last page" is equal to $midRange
if (($this->currentPage < ($this->numPages - $this->midRange) - 1)) { // if number of pages between $currentPage and "last page" exceeds $range with more than two
$lastPage .= '…'; // add "..." between "last page"-link and last page in $range
}
$lastPage .= '<a href="'.$this->queryString.'&'.$this->pageIdentifier.'='.$this->numPages.'" class="last">'.$this->numPages.'</a>'; // show "last page"-link
}
return $lastPage;
}
# Range of pages between (prev first ...) and (... last next)
public function listPages()
{
for ($i = ($this->currentPage - $this->midRange); $i < (($this->currentPage + $this->midRange) + 1); $i++){
if (($i > 0) && ($i <= $this->numPages)) // if page number are within page range
{
if ($i == $this->currentPage) { $listPages .= '<a class="current">'.$i.'</a>'; } // if we're on current page
else { $listPages .= '<a href="'.$this->queryString.'&'.$this->pageIdentifier.'='.$i.'">'.$i.'</a>'; } // if not current page
}
}
return $listPages;
}
This is what I do for my Pagination.
$startPage = $currentPage - 4;
$endPage = $currentPage + 4;
if ($startPage <= 0) {
$endPage -= ($startPage - 1);
$startPage = 1;
}
if ($endPage > $totalPage)
$endPage = $totalPage;
if ($startPage > 1) echo " First ... ";
for($i=$startPage; $i<=$endPage; $i++) echo " {$i} ";
if ($endPage < $totalPage) echo " ... Last ";
I believe my code is self-explained, but I will try to explain it in plain English. First of all, you need to know two things before you can generate Pagination: $totalPage and $currentPage.
Step 1: Assuming that the current page is in mid-range. $startPage and $endPage store range of page that pagination try to generate.
Step 2: If $startPage is negative, then you need to make-up for $endPage.
Step 3: If $endPage excess $totalPage, then $endPage is the last page.
Step 4: Generating Pagination into HTML. (it is up to you how you want your pagination to look. I will simply use plain text to represent my pagination)
if ($startPage > 1) echo " First ... ";
for($i=$startPage; $i<=$endPage; $i++) echo " {$i} ";
if ($endPage < $totalPage) echo " ... Last ";
Fixed flaw to my previous logic
$startPage = ($curPage < 5)? 1 : $curPage - 4;
$endPage = 8 + $startPage;
$endPage = ($totalPage < $endPage) ? $totalPage : $endPage;
$diff = $startPage - $endPage + 8;
$startPage -= ($startPage - $diff > 0) ? $diff : 0;
if ($startPage > 1) echo " First ... ";
for($i=$startPage; $i<=$endPage; $i++) echo " {$i} ";
if ($endPage < $totalPage) echo " ... Last ";
这篇关于逻辑背后的分页就像google的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!