这可能是个很傻的问题,但我想不出任何可能帮助我进一步发展的东西。我希望缩短我的页面导航中的数字数量。
而不是像: 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>发布于 2012-08-11 21:42:51
这是我为分页论坛编写的修改代码块。
它的输出并不是你所显示的那样,而应该只是一个调整的问题。
<?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; ?>发布于 2012-08-11 21:46:46
看看这个:https://codereview.stackexchange.com/a/10292。这个解决方案应该能解决你的问题。我认为这是一个很好的方法。
发布于 2012-08-11 22:24:32
该链接显示当前请求链接之前的两个链接和当前请求的链接之后的两个链接:
<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
通过添加适当的条件,添加前五个链接和最后五个链接也不是那么困难。
这里的测试脚本
https://stackoverflow.com/questions/11917775
复制相似问题