Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分页背后的逻辑,比如google_Php_Oop_Class_Pagination_Paginate - Fatal编程技术网

Php 分页背后的逻辑,比如google

Php 分页背后的逻辑,比如google,php,oop,class,pagination,paginate,Php,Oop,Class,Pagination,Paginate,谷歌分页行为背后的逻辑是什么 我的分页器是这样的: [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 ..

谷歌分页行为背后的逻辑是什么

我的分页器是这样的:

[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   >
[1]23。。。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   >
以下是上述示例的实时版本:。
我知道为什么会这样;我已经将当前页面两侧显示的页码数量设置为两(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   >
    $paginator->set_resultsPerPage((int));
    $paginator->set_midRange((int));
    $paginator->set_pageIdentifier('querystring-pageNumber-identifier-name-for-get');  //  whatever I needed
    $paginator->pageController('full');  //  full, med, min for different styles.
    $paginator->prevPage();
    $paginator->firstPage();
    $paginator->listPages();
    $paginator->lastPage();
    $paginator->nextPage();
    $paginator->pageJumper();
    $paginator->perPageSelector();
1  -> [1] 2 3 4 5
2  -> 1 [2] 3 4 5
3  -> 1 2 [3] 4 5
..
5  -> 3 4 [5] 6 7
6  -> 4 5 [6] 7 8
..
8  -> 6 7 [8] 9 10
9  -> 6 7 8 [9] 10
10 -> 6 7 8 9 [10]
[1]23 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   >
在开始和结束时,我需要做一些更改,但不知道如何使其易于操作…
我也想让它灵活一些。这意味着我想能够改变每一面想要的页面数量,并让脚本展开和计算所有

这是到目前为止我的代码:

/**
 *  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 .= '&hellip;';  //  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 .= '&hellip;';  //  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; 
}
/**
*页面控制器按钮
*@param str$this->querySting href=“URL string”
*@param str$this->pageIdentifier$\u GET['this-name']
*@param int$this->numPages总页数
*@param int$this->当前页面每侧显示的中间页数
*/
公共功能页()
{
如果($this->currentPage>1){
$prevPage=($this->currentPage-1);
返回“”;
}
}
公共功能下页()
{
如果($this->currentPage<$this->numPages){
$nextPage=$this->currentPage+1;
返回“”;
}  
}
公共函数首页()
{
如果($this->currentPage>($this->middrange+1)){//如果“currentPage”和“firstPage”之间的页数超过$middrange,则为1。。。
$firstPage.='';/…显示“第一页”-链接
如果($this->currentPage>($this->middrange+2)){//如果$currentPage和“first page”之间的页数超过$middrange且超过1
$firstPage.='&hellip;';//在“第一页”-链接和$range中的第一页之间添加“…”
}
}
返回$firstPage;
}
公共函数lastPage()
{
如果($this->currentPage<($this->numPages-$this->middrange)){//如果“currentPage”和“last page”之间的页数等于$middrange
如果($this->currentPage<($this->numPages-$this->middrange)-1)){//如果$currentPage和“最后一页”之间的页数超过$range且超过两页
$lastPage.='&hellip;';//在“最后一页”-链接和$range中的最后一页之间添加“…”
} 
$lastPage.='';//显示“最后一页”-链接
}
返回$lastPage;
}
#介于(前一页…)和(后一页…)之间的页面范围
公共函数列表页()
{
对于($i=($this->currentPage-$this->middrange);$i<($this->currentPage+$this->middrange)+1);$i++){
if($i>0)&&($i numPages))//如果页码在页面范围内
{
如果($i==$this->currentPage){$listPages.='';}//如果不是当前页面
}
}
返回$listPages;
}

我假设您的分页具有以下结构:

活动页面的数量+单独的(…)+页面(184)+下一页(>)

您可以将活动页面的数量设置为8 (包括上一页)(
这是我分页的工作

$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 ";
$startPage=$currentPage-4;
$endPage=$currentPage+4;
如果($startPage$totalPage)
$endPage=$totalPage;
如果($startPage>1)回显“第一个…”;
对于($i=$startPage;$i 1)回显“第一…”;
对于($i=$startPage;$i 0)?$diff:0;
如果($startPage>1)回显“第一个…”;

对于($i=$startPage;$iHear是分页显示的一个简单示例:

$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is not equal to 1, 
// if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
  // This shows the user what page they are on, and the total number of pages
  $paginationDisplay .= 'Page <strong>' . $pn . 
            '</strong> of ' . $lastPage. 'last';
  // If we are not on page 1 we can place the Back button
  if ($pn != 1) {
     $previous = $pn - 1;
     $paginationDisplay .=  '&nbsp;  <a href="' . 
            $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    } 
    // Lay in the clickable numbers display here between the Back and Next links
    $paginationDisplay .= '<span>' . $centerPages . '</span>';
    // If we are not on the very last page we can place the Next button
    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . 
            $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    } 
}
$paginationDisplay=”“;//初始化分页输出变量
//仅当最后一页变量不等于1时,此代码才会运行,
//如果只有1页,我们不需要分页链接来显示
如果($lastPage!=“1”){
//这将向用户显示他们所在的页面以及页面总数
$paginationDisplay.='Page。$pn。
“的“$lastPage.”last';
//如果我们不在第1页,我们可以放置后退按钮
如果($pn!=1){
$previous=$pn-1;
$paginationDisplay.='';
} 
//将此处显示的可点击数字放在后面和下一个链接之间
$paginationDisplay.=''.$centerPages';
//如果我们不在最后一页,我们可以放置“下一步”按钮
如果($pn!=$lastPage){
$nextPage=$pn+1;
$paginationDisplay.='';
} 
}

这真是太棒了!我想我让这个分页器按照我描述的方式工作。
请看一看,在这里尝试一下,让我知道

经过大量的逻辑数学研究,我终于得出了这个结论:
为了使这一行为在不同的层面上有如此不同的表现,必须有一些
if
elsef
-s来分别处理每个层面上的逻辑。我将尝试解释,但发现很难以一种好的方式来做

以下是我所说的级别:

  • 如果currentPage==第一页:
    计算从第二页开始的当前页之后要显示的页数。
    这个计算需要根据最多有多少个页面框来完成。(中间值是一个关键因素)

  • elseif currentPage介于第一页和最大值的中间值之间。
    将范围内的页面减少1到pr
    $paginationDisplay = ""; // Initialize the pagination output variable
    // This code runs only if the last page variable is not equal to 1, 
    // if it is only 1 page we require no paginated links to display
    if ($lastPage != "1"){
      // This shows the user what page they are on, and the total number of pages
      $paginationDisplay .= 'Page <strong>' . $pn . 
                '</strong> of ' . $lastPage. 'last';
      // If we are not on page 1 we can place the Back button
      if ($pn != 1) {
         $previous = $pn - 1;
         $paginationDisplay .=  '&nbsp;  <a href="' . 
                $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
        } 
        // Lay in the clickable numbers display here between the Back and Next links
        $paginationDisplay .= '<span>' . $centerPages . '</span>';
        // If we are not on the very last page we can place the Next button
        if ($pn != $lastPage) {
            $nextPage = $pn + 1;
            $paginationDisplay .=  '&nbsp;  <a href="' . 
                $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
        } 
    }
    
    [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  ...  178  179 [180] 181  182  ...   184   >
    
    <   1  ...  178  179  180 [181] 182  183   184   >
    <   1  ...  178  179  180  181 [182] 183   184   >
    <   1  ...  178  179  180  181  182 [183]  184   >
    
        <   1  ...  178  179  180  181  182  183  [184]  >
    
        $paginator = new paginator((int));  //  e.g. number of total results from a db request
    
        $paginator->set_queryString('my querystring');
    
        $paginator->set_resultsPerPage((int));
        $paginator->set_midRange((int));
        $paginator->set_pageIdentifier('querystring-pageNumber-identifier-name-for-get');  //  whatever I needed
    
        $paginator->pageController('full');  //  full, med, min for different styles.
    
        $paginator->prevPage();
        $paginator->firstPage();
        $paginator->listPages();
        $paginator->lastPage();
        $paginator->nextPage();
        $paginator->pageJumper();
        $paginator->perPageSelector();
    
    $pLinks = 5; // Links per page 
    $pMids = 3;  
    $pTot = 10; // Total page 
    $pSel = 1  // Selected page 
    
    if (($pSel <= $pMids) || ($pTot <= $pLinks)) {
        $sPage = 1;                
        $ePage = ($pTot <= $pLinks) ? $pTot : $pLinks;
    } else {
        $etPage = $pSel + ($pMids - 1);            
        $ePage = ($etPage <= $pTot) ? $etPage : $pTot;            
        $sPage = $ePage - ($pLinks - 1);            
    }
    
    if ($pSel > $sPage) {
        $sL = '<a href="#" id="1">First</a>';
        $sN = '<a href="#" id="'.($pSel-1).'">&laquo;</a>';
    } else {
        $sL = 'First';
        $sN = '&laquo;';
    }
    
    if ($pSel < $ePage) {
        $eL = '<a href="#" id="'.$pTot.'">End</a>';
        $eN = '<a href="#" id="'.($pSel+1).'">&raquo;</a>';
    } else {
        $eL = 'End';
        $eN = '&raquo;';
    }
    
    $pOptions = '';
    
    $pOptions .= '<span class="iPage">'.$pSel.'/'.$pTot.'</span>';
    $pOptions .= '<span class="renderFL">'.$sL.'</span>';
    $pOptions .= '<span class="renderPN">'.$sN.'</span>';
    
    for ($i = $sPage; $i <= $ePage; $i++) {
        if($i != $pSel) {
            $pOptions .= '<span><a href="#" id="'.$i.'">'.$i.'</a></span>';
        } else {
            $pOptions .= '<span class="selected">'.$i.'</span>';
        }
    }
    
    $pOptions .= '<span class="renderPN">'.$eN.'</span>';
    $pOptions .= '<span class="renderFL">'.$eL.'</span>';
    
    1  -> [1] 2 3 4 5
    2  -> 1 [2] 3 4 5
    3  -> 1 2 [3] 4 5
    ..
    5  -> 3 4 [5] 6 7
    6  -> 4 5 [6] 7 8
    ..
    8  -> 6 7 [8] 9 10
    9  -> 6 7 8 [9] 10
    10 -> 6 7 8 9 [10]
    
     <  [1]   2    3    4    5    6   7    ...   99   >
     <   1   [2]   3    4    5    6   7    ...   99   >
     <   1    2   [3]   4    5    6   7    ...   99   >
     <   1    2    3   [4]   5    6   7    ...   99   >
     <   1    2    3    4   [5]   6   7    ...   99   >
     <   1   ...   4    5   [6]   7   8    ...   99   >
     <   1   ...   5    6   [7]   8   9    ...   99   >
     <   1   ...   92   93  [94]  95  96   ...   99   >
     <   1   ...   93   94  [95]  96  97   98    99   >
     <   1   ...   93   94   95  [96] 97   98    99   >
     <   1   ...   93   94   95   96 [97]  98    99   >
     <   1   ...   93   94   95   96  97  [98]   99   >
     <   1   ...   93   94   95   96  97   98   [99]  >
    
    get_pages_array = (total_page, each_side, curr_page) ->
        if total_page <= (2*each_side)+5
            # in this case, too few pages, so display them all
            start_page = 1
            end_page = total_page
        else if curr_page<=each_side+3
            # in this case, curr_page is too close to the beginning
            start_page = 1
            end_page = (2*each_side)+3
        else if curr_page >= total_page - (each_side+2)
            # in this case, curr_page is too close to the end
            start_page = total_page - (2*each_side) - 2
            end_page = total_page
        else
            # regular case
            start_page = curr_page - each_side
            end_page = curr_page + each_side
        return_me = []
        if start_page> 1
            return_me.push "1"
        if start_page>2
            return_me.push "..."
        for x in [start_page..end_page]
            return_me.push x
        if end_page<total_page-1
            return_me.push "..."
        if end_page<total_page
            return_me.push total_page
        return return_me
    
    def main():
        num_pages = 13
        page = 12
    
        window = 5
        start = page - window
        end = page + window - 1
        if start <= 0:
            end = end - start + 1
            start = 1
        if end > num_pages:
            end = num_pages
            start = max(end - (window * 2) + 1, 1)
    
        for no in range(start, end + 1):
            print "{}*".format(no) if page == no else no
    
    if __name__ == '__main__':
        main()