2012-08-11 98 views
0

这可能是一个很愚蠢的问题,但我无法想出任何可能会帮助我进一步研究的问题。我正在寻找缩短我的页面导航中的NUMBERS数量。截取页数的分页

而不是被像:1,2,3,4,5,6,7,8

我希望它是这样:1,2,... 7,8

当我去,现在应该在数字组中看到数字。

这里是我的代码,负责页码:

<div id="user_nav_bottom"> 
<?php for($i=1; $i < sizeof($pages)+1; $i++) { 
    $strong = ($_GET['page'] == $i) ? ' dark bold' : ''; 
    echo '<span class="normal' . $strong . '"><a href="imgit_images.php?page=' . $i . '">' . $i . ', </a></span>'; 
} ?> 
</div> 
+0

究竟是什么问题?成为会员近2年,并提出近50个问题,你现在应该知道如何问一个好问题。 – Bojangles 2012-08-11 21:27:42

+0

是的,但我倾向于被频繁地阻挡到简单的东西,无论我多么努力地想到一个解决方案,我都无法自己得到它,所以我在这里问。我想我描述我的问题有点好,告诉我你不明白什么? – Aborted 2012-08-11 21:29:40

+0

我不明白你有什么问题。你无法弄清楚如何截断页码,但你坚持了什么_exactly_? – Bojangles 2012-08-11 21:30:21

回答

2

这是我写的分页论坛的代码修改的块。

它的输出并不完全如你所示,但应该只是一个调整问题。

<?php 
    //Set up vars 

    $currentPage = isset($_GET["page"])? $_GET["page"] : 1; 
    $numPages = count($pages); 
    $numPages = 7;//cause I don't have the real var for testing 

    $howMany = 1; 
?> 


<?php if ($numPages > 1): ?> 
    <li>Page <?php echo $currentPage?> of <?php echo $numPages?></li> 
    <?php if ($currentPage > 1): ?> 
     <li><a href="imgit_images.php?page=1">First</a></li> 
     <li><a href="imgit_images.php?page=<?php echo $currentPage-1?>">&lt;</a></li> 
    <?php endif; ?> 

    <?php if ($currentPage > $howMany + 1): ?> 
     <li>...</li> 
    <?php endif; ?> 

    <?php for ($pageIndex = $currentPage - $howMany; $pageIndex <= $currentPage + $howMany; $pageIndex++): ?> 
     <?php if ($pageIndex >= 1 && $pageIndex <= $numPages): ?> 
      <li> 
       <?php if ($pageIndex == $currentPage): ?> 
        <u> 
       <?php endif; ?> 

       <a href="imgit_images.php?page=<?php echo $pageIndex?>"><?php echo $pageIndex?></a> 

       <?php if ($pageIndex == $currentPage): ?> 
        </u> 
       <?php endif; ?> 
      </li> 
     <?php endif; ?> 
    <?php endfor; ?> 

    <?php if ($currentPage < $numPages - $howMany): ?> 
     <li>...</li> 
    <?php endif; ?> 

    <?php if ($currentPage < $numPages): ?> 
     <li><a href="imgit_images.php?page=<?php echo $currentPage+1?>">&gt;</a></li> 
     <li><a href="imgit_images.php?page=-1">Last</a></li> 
    <?php endif; ?> 
<?php endif; ?> 
2

这其中显示了两个链接和两个当前请求的链接前后:

<div id="user_nav_bottom"> 
<?php 
$current = $_GET['page']; 
$last = count($pages)+1; 
$curr0 = $current-2; 
$curr1 = $current+2; 
if ($curr0<=1) { 
    $curr0 = 1; 
    $curr1 = $last>5? 5 : $last; 
} 
if ($curr1>=$last) { 
    $curr0 = $last-4 < 1 ? 1 : $last-4; 
    $curr1 = $last; 
} 
// now print all links: 
echo '<a href="imgit_images.php?page=1">&#171;</a> '; 
for ($i=$curr0; $i<=$curr1; $i++) { 
    $style = ($i==$current)? 'font-weight:bold':''; 
    echo ' <a href="imgit_images.php?page='.$i.'" style="'.$style.'">'.$i.'</a> '; 
} 
echo '<a href="imgit_images.php?page='.$last.'">&#187;</a> '; 
?> 
</div> 

这说明像这样的链接:«13 14 16 17»
恕我直言,这不是那么困难也增加前五个和后五个链接添加适当的条件。

Testing script here