【问题标题】:pagination php only 10 items分页php只有10个项目
【发布时间】:2013-10-15 18:39:13
【问题描述】:

我的分页有问题,如何只显示 10 个项目?可以在谷歌做这样的事情 - 数字增加了+5?

<?php
// Make the links to other pages, if necessary.
if ($pages > 1) {

    echo '<br /><p>';
    $current_page = ($start/$display) + 1;

    // If it's not the first page, make a Previous button:
    if ($current_page != 1) {
        echo '<a href="list_photos_4.php?s=' . ($start - $display) . '&p=' . $pages . '&sort=' . $sort . '">Previous</a> ';
    }

    // Make all the numbered pages:
    for ($i = 1; $i <= $pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="list_photos_4.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';
        } else {
            echo $i . ' ';
        }
    } // End of FOR loop.

    // If it's not the last page, make a Next button:
    if ($current_page != $pages) {
        echo '<a href="list_photos_4.php?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>';
    }

    echo '</p>'; // Close the paragraph.

} // End of links section.
?>

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    尝试将部分(使所有编号的页面)更改为:

    // Make all the numbered pages:
        for ($i = 1; $i <= $pages; $i++) {    
            if ($i != $current_page) {
                $distance = $current_page - $i;
                if (abs($distance) < 5){
                    echo '<a href="list_photos_4.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';
                } 
            } else {
                echo $i . ' ';
            }
        } // End of FOR loop
    

    我希望这对你有用。

    【讨论】:

    • 你好,谢谢你现在的回复我有类似这样的东西第 186 行 - 第 186 行"如果 ($i != $current_page) {"
    • @Mantykora7 只是将我的解决方案放入 for 循环中: for ($i = 1; $i
    • 是的,您是对的,粘贴了一些错误,效果很好,感谢您的时间和回答:)
    • @Mantykora7 我很高兴能帮上忙 :) 感谢您批准我的回答。 PS proszę dodaj jeszcze 1 w górę - odemnie masz + :)
    【解决方案2】:
    <?php
    // Make the links to other pages, if necessary.
    if ($pages > 1) {
    
        // (...)
    
        $first_disp_page = $cutrrent_page - 5 < 1 ? 1 : $current_page - 5;
        $last_disp_page = $current_page + 5 > $pages ? $pages : $current_page + 5;
    
        // Make all the numbered pages:
        for ($i = $first_disp_page; $i <= $last_disp_page; $i++) {
            if ($i != $current_page) {
                echo '<a href="list_photos_4.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';
            } else {
                echo $i . ' ';
            }
        } // End of FOR loop.
    
        // (...)
    
    } // End of links section.
    ?>
    

    【讨论】:

    • 感谢您的宝贵时间和回答:)
    【解决方案3】:

    改变这个:

    // Make all the numbered pages:
    for ($i = 1; $i <= $pages; $i++) {
    

    到这里:

    if ($current_page <= 5) $first_page = 1;
    else if ($pages - $current_page < 10) $first_page = $pages - 10;
    else $first_page = $current_page - 5;
    
    // Make up to 10 numbered pages:
    for ($i = $first_page; $i < ($first_page + 10); $i++) {
    

    【讨论】:

    • 你好 - 当 $display = 2 时工作正常; - items 25,当 $display = 10 items 10 它显示类似这样的内容 Previous -5 -4 -3 -2 -1 0 1 2 3 4 Next
    • 我不确定 $display 的用途。也就是说,$current_page = ($start/$display) + 1; 似乎很奇怪...... $current_page 应该只是(int) $_GET['s'],虽然老实说我很难理解你的链接结构。您应该只传递排序顺序和页码,页数应由服务器确定,而不是由浏览器发送。你应该清理你的整体代码/逻辑以找出细节。
    • 感谢您的时间和回答 - 已经解决了问题
    猜你喜欢
    • 2017-10-20
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2011-08-27
    • 2015-06-12
    相关资源
    最近更新 更多