Truncate number of pages in pagination

前端 未结 4 952
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 18:44

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.

相关标签:
4条回答
  • 2021-01-16 19:23

    What you might want to do is use the "logarithmic" pagination technique described in my answer here:

    How to do page navigation for many, many pages? Logarithmic page navigation

    If you set LINKS_PER_STEP to a low value like 2 or 3, it'll produce very compact output.

    0 讨论(0)
  • 2021-01-16 19:31

    This one shows two links before and two after the current requested link:

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

    This shows links like this: « 13 14 15 16 17 »
    Imho it's not so difficult to add also first five and last five links by adding appropriate conditions.

    Testing script here

    0 讨论(0)
  • 2021-01-16 19:35

    Check this out: https://codereview.stackexchange.com/a/10292. This solution should solve your problem. I think its a very good approach.

    0 讨论(0)
  • 2021-01-16 19:47

    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; ?>
    
    0 讨论(0)
提交回复
热议问题