문제

그래서 저는 PHP 에서이 작업을 수행하고 있지만 논리 문제이므로 가능한 한 일반적으로 쓰려고 노력할 것입니다.

시작하려면이 페이지 매김 스크립트가 작동하는 방법이 있습니다.

  1. 을 위한 (첫 세 페이지 링크를 그립니다)
  2. 만약에 (#1 페이지와 #3 페이지 사이에 페이지가있는 경우 Ellipsis (...) 그리기)
  3. 을 위한 (링크의 양쪽에 현재 페이지와 두 페이지를 그립니다.)
  4. 만약에 (#3 페이지와 #5 페이지 사이에 페이지가있는 경우 elipsis (...) 그리기)
  5. 을 위한 (마지막 3 페이지 링크를 그립니다)

문제는 낮은 양의 페이지가있을 때 (페이지 수가 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_count 및 $ current_page를 반복하여 예제 출력 (수정)이 있습니다.http://rafb.net/p/tna56h71.html

도움이 되었습니까?

해결책

<?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] 

다른 팁

이것은 아마도 복잡한 솔루션이지만 작동합니다.

나는 단지 인쇄 대신 배열을 사용하여 논리를 "do-over"할 수 있습니다.

문제의 일부는 "페이지의 왼쪽과 오른쪽"이 왼쪽과 오른쪽 어깨와 일치 할 때 발생합니다.

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;

}

출력은 여기에서 볼 수 있습니다. http://dpaste.com/92648/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top