[英]Truncate number of pages in pagination
這可能是一個很愚蠢的問題,但是我想不出任何可以幫助我進一步發展的東西。 我希望縮短頁面導航中的NUMBERS個。
而不是像這樣: 1、2、3、4、5、6、7、8
我希望它像: 1、2 ... 7、8
當我轉到2時 ,現在應該在數字組中看到3號。
這是我的負責頁碼的代碼:
<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>
這是我為分頁論壇編寫的修改后的代碼塊。
它的輸出與您所顯示的不完全相同,而僅需進行調整即可。
<?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?>"><</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?>">></a></li>
<li><a href="imgit_images.php?page=-1">Last</a></li>
<?php endif; ?>
<?php endif; ?>
檢查一下: https : //codereview.stackexchange.com/a/10292 。 此解決方案應該可以解決您的問題。 我認為這是一種非常好的方法。
此鏈接顯示當前請求的鏈接之前的兩個鏈接和之后的兩個鏈接:
<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">«</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.'">»</a> ';
?>
</div>
這顯示了這樣的鏈接:«13 14 15 16 17»
恕我直言,通過添加適當的條件來添加前五個和后五個鏈接並不難。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.