分页问题[PHP]

分页问题[PHP],php,pagination,logic,Php,Pagination,Logic,所以我用PHP来做这件事,但这是一个逻辑问题,所以我将尝试尽可能一般地编写它 以下是分页脚本的工作原理: for(绘制前三页链接) if(如果在#1的页面和#3的页面之间有页面,则绘制省略号(…) for(绘制当前页面及其两侧的两页链接) if(如果第3页和第5页之间有页,则绘制省略号(…) 用于(绘制最后三页链接) 问题是,当页面数量较少时(我在页面计数为10时注意到这一点),应该有一个省略号,但不绘制任何页面 关于代码: $page_count = 10; //in actual code

所以我用PHP来做这件事,但这是一个逻辑问题,所以我将尝试尽可能一般地编写它

以下是分页脚本的工作原理:

  • for(绘制前三页链接)
  • if(如果在#1的页面和#3的页面之间有页面,则绘制省略号(…)
  • for(绘制当前页面及其两侧的两页链接)
  • if(如果第3页和第5页之间有页,则绘制省略号(…)
  • 用于(绘制最后三页链接)
  • 问题是,当页面数量较少时(我在页面计数为10时注意到这一点),应该有一个省略号,但不绘制任何页面

    关于代码:

    $page_count = 10; //in actual code this is set properly
    $current_page = 1; //in actual code this is set properly
    
    for ($i = 1;$i <= 3;$i++)
    {
        if ($page_count >= $i)
            echo $i;
    }
    
    if ($page_count > 3 && $current_page >= 7)
        echo "...";
    
    for ($i = $current_page - 2;$i <= current_page + 2;$i++)
    {
        if ($i > 3 && $i < $page_count - 2)
            echo $i;
    }
    
    if ($page_count > 13 && $current_page < $page_count - 5)
        echo "...";
    
    for ($i = $page_count - 2;$i <= $page_count;$i++)
    {
        if ($page_count > 3)
            echo $i;
    }
    
    $page\u count=10//在实际代码中,这是正确设置的
    $current_page=1//在实际代码中,这是正确设置的
    对于($i=1;$i=$i)
    echo$i;
    }
    如果($page\u count>3&&$current\u page>=7)
    回声“…”;
    对于($i=$current\u page-2;$i3&&$i<$page\u count-2)
    echo$i;
    }
    如果($page_count>13&&$current_page<$page_count-5)
    回声“…”;
    对于($i=$page_count-2;$i 3)
    echo$i;
    }
    
    所以我想最好的办法是修改两个省略号if语句中的一个,以包括这样一个案例,然而我已经尝试过了,并且被难倒了

    另外请注意,为了可读性,我压缩了这段代码,所以请不要给出类似“那些for循环无效,因为它们将重新计算当前的_页面-每次迭代2”这样的提示,因为我知道:)


    对于那些希望看到此逻辑当前工作方式的细分的人,下面是迭代$page_count和$current_page的示例输出(修改)。

    这可能是一个过于复杂的解决方案,但它确实有效

    我在这里使用了一个数组,而不仅仅是打印,这让我可以“重写”逻辑

    部分问题发生在“页面的左侧和右侧”恰好与左右肩部重合时

    function cdotinator ( $current_page, $page_count ) 
    {
      $stepsize = 3; 
      $elipse = '...';
      # Simple Case. 
      if ( $page_count <= 2 * $stepsize )
      {
        $out = range( 1, $page_count );
        $out[$current_page - 1 ] = '*' . $current_page . '*';
        return $out;
      }
      #Complex Case
      # 1) Create All Pages
      $out = range( 1, $page_count ); 
      # 2 ) Replace "middle" pages with "." placeholder elements 
      for( $i = $stepsize+1 ; $i <= ( $page_count - $stepsize ) ; $i ++ )
      {
        $out[ $i - 1 ] = '.' ; 
      }
      # 3.1 ) Insert the pages around the current page 
      for( $i =  max(1,( $current_page - floor($stepsize / 2) )) ;
           $i <= min( $page_count,( $current_page + floor( $stepsize/2))); 
           $i ++ )
      {
        $out[ $i - 1] = $i;
      }
      # 3.2 Bold Current Item
      $out[ $current_page - 1 ] = '*' . $current_page . '*' ; 
    
      # 4 ) Grep out repeated '.' sequences and replace them with elipses 
      $out2 = array(); 
      foreach( $out as $i => $v )
      {
        #  end, current  == peek() 
        end($out2);
        if( current($out2) == $elipse and $v == '.' )
        {
            continue;
        }
        if( $v == '.' )
        {
          $out2[] = $elipse; 
          continue;
        }
        $out2[]= $v;
      }
    
      return $out2;
    
    }
    
    函数cdotinator($current\u page,$page\u count)
    {
    $stepsize=3;
    $elipse=“…”;
    #简单的例子。
    
    如果($page_count
    这不是PHP问题!请不要添加PHP标记,因为代码示例甚至不是用PHP编写的!您可能希望提供工作示例代码,以便希望自己测试您的逻辑的人不必用自己的语言重新实现您的“psuedo”代码,这样他们就可以测试它的任何功能。好的,我将其更改为PHP code:/回答得很好——我现在使用的是一个稍微淡化的版本,因为我不需要它作为函数,因为您的一些变量是我的常量。请注意,有一个bug;您需要将第25行和第27行的“-1”和“+1”分别更改为“-2”和“+2”
    
    <?php
    
    /**
     * windowsize must be odd
     *
     * @param int $totalItems 
     * @param int $currentPage 
     * @param int $windowSize 
     * @param int $anchorSize 
     * @param int $itemsPerPage 
     * @return void
     */
    function paginate($totalItems, $currentPage=1, $windowSize=3, $anchorSize=3, $itemsPerPage=10) {
        $halfWindowSize = ($windowSize-1)/2;
    
        $totalPages = ceil($totalItems / $itemsPerPage);
        $elipsesCount = 0;
        for ($page = 1; $page <= $totalPages; $page++) {
            // do we display a link for this page or not?
            if ( $page <= $anchorSize ||  
                $page > $totalPages - $anchorSize ||
                ($page >= $currentPage - $halfWindowSize &&
                $page <= $currentPage + $halfWindowSize) ||
                ($page == $anchorSize + 1 &&
                 $page == $currentPage - $halfWindowSize - 1) ||
                ($page == $totalPages - $anchorSize &&  
                 $page == $currentPage + $halfWindowSize + 1 ))
            {
                $elipsesCount = 0;
                if ($page == $currentPage)
                    echo ">$page< ";
                else
                    echo "[$page] ";
            // if not, have we already shown the elipses?
            } elseif ($elipsesCount == 0) {
                echo "... ";
                $elipsesCount+=1; // make sure we only show it once
            }
        }
        echo "\n";
    }
    
    //
    // Examples and output
    //
    
    paginate(1000, 1, 3, 3);
    // >1< [2] [3] ... [98] [99] [100] 
    
    paginate(1000, 7, 3, 3);
    // [1] [2] [3] ... [6] >7< [8] ... [98] [99] [100] 
    
    paginate(1000, 4, 3, 3);
    // [1] [2] [3] >4< [5] ... [98] [99] [100] 
    
    paginate(1000, 32, 3, 3);
    // [1] [2] [3] ... [31] >32< [33] ... [98] [99] [100] 
    
    paginate(1000, 42, 7, 2);
    // [1] [2] ... [39] [40] [41] >42< [43] [44] [45] ... [99] [100]