沃梦达 / 编程问答 / php问题 / 正文

截断分页中的页数

Truncate number of pages in pagination(截断分页中的页数)

本文介绍了截断分页中的页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常愚蠢的问题,但我想不出任何可以帮助我走得更远的东西.我希望缩短页面导航中 NUMBERS 的数量.

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

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

当我转到 2 时,数字 3 现在应该会出现在数字组中.

这是我负责页码的代码:

<?php for($i=1; $i < sizeof($pages)+1; $i++) {$strong = ($_GET['page'] == $i) ?'深色粗体':'';echo '<span class="normal' . $strong . '"><a href="imgit_images.php?page='. $i .'">'.$i .', </a></span>';?>

解决方案

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

它的输出并不完全是你展示的方式,而应该只是调整的问题.

<?php if ($numPages > 1): ?><li>Page <?php echo $currentPage?><?php echo $numPages?></li><?php if ($currentPage > 1): ?><li><a href="imgit_images.php?page=1">首先</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): ?><你><?php endif;?><a href="imgit_images.php?page=<?php echo $pageIndex?>"><?php echo $pageIndex?></a><?php if ($pageIndex == $currentPage): ?></u><?php endif;?><?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">最后</a></li><?php endif;?><?php endif;?>

This might be a quite silly question, but I can't figure anything out that might help me go further. I'm looking to shorten the number of NUMBERS in my page navigation.

Instead of being like: 1, 2, 3, 4, 5, 6, 7, 8

I want it be like: 1, 2...7, 8

And when I go to 2, number 3 should now be seen in the numbers group.

Here is my code that is in charge of the page numbers:

<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>

解决方案

This is a modified chunk of code I wrote for paging forums.

It's output isn't exactly how you showed but should just be a matter of tweaking.

<?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; ?>

这篇关于截断分页中的页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:截断分页中的页数

基础教程推荐