首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >截断分页中的页数

截断分页中的页数
EN

Stack Overflow用户
提问于 2012-08-11 21:23:53
回答 4查看 4.2K关注 0票数 0

这可能是个很傻的问题,但我想不出任何可能帮助我进一步发展的东西。我希望缩短我的页面导航中的数字数量。

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

我想要的是: 1,2.7,8

当我进入2,数字3现在应该出现在数字组中。

下面是负责页码的代码:

代码语言:javascript
复制
<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>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-11 21:42:51

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

它的输出并不是你所显示的那样,而应该只是一个调整的问题。

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2012-08-11 21:46:46

看看这个:https://codereview.stackexchange.com/a/10292。这个解决方案应该能解决你的问题。我认为这是一个很好的方法。

票数 2
EN

Stack Overflow用户

发布于 2012-08-11 22:24:32

该链接显示当前请求链接之前的两个链接和当前请求的链接之后的两个链接:

代码语言:javascript
复制
<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 15 16 17

通过添加适当的条件,添加前五个链接和最后五个链接也不是那么困难。

这里的测试脚本

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11917775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档