2013-02-07 37 views
0

所以我有我这样的分页链接。PHP分页 - 链接数量有限

for ($counter = 0; $counter <= $page_amount; $counter += 1) { 
     echo "<a href=\"section.php?q=$section&p=$counter\">"; 
     echo $counter+1; 
     echo "</a>"; 
    } 

和连杆变得像:

1 2 3 4 5 6 7等。

,但我想这个限制,所以如果有超过7页应只显示7个环节是这样的:

1 2 3 ... 10 11 12

,其中12是最后一页。

如果你去到下一个网页时,才会改变第一页这样的:

3 4 5 ... 10 11 12

,直到你到达最后7页是这样的:

我该怎么做?

请大家帮忙。

+4

[你尝试过什么?](http://www.whathaveyoutried.com/)参见[问咨询】(HTTP: //stackoverflow.com/questions/ask-advice),请。 –

+0

我第一次访问这个网站,我得到这个很多的帮助= /耶!我想这是一个时间,然后.. – user2049048

+0

如果你期望与显示任何尝试修复你的自我的帮助,那么是访问其他地方。 – 2013-02-07 01:31:30

回答

0

这是一种方法。

// Set some presets 
$current_page = 0; 
$page_amount = 11; 
$limiter = 7; 

// Set upper and lower number of links 
$sides = round(($limiter/2), 0, PHP_ROUND_HALF_DOWN); 

for ($counter = 0; $counter <= $page_amount; $counter++) { 
    // Start with Current Page 
    if($counter >= ($current_page)){ 
     // Show page links of upper and lower 
     if(($counter <($current_page+$sides))||($counter >($page_amount-$sides))){ 
      echo "<a href=\"section.php?q=$section&p=$counter\">"; 
      echo $counter+1; 
      echo "</a> "; 
     } 
     // The middle link 
     elseif($counter ==($current_page+$sides)){ 
      echo "<a href=\"page.php?p=$counter\">"; 
        // Show number if number of links == $limiter 
      if(($page_amount-$current_page)==$limiter-1){ 
       echo $counter+1; 
      } 
      // Show '...' number of links > $limiter 
        else { 
        echo "..."; 
      } 
      echo "</a> "; 
     } 
    } 
} 

这允许改变显示的链接的数量,即, 7至9

注意,在使用round()需要PHP_ROUND_HALF_DOWN PHP> = 5.3