PHP和MySQL分页更新帮助

4

我有段时间没有更新网页上的分页了,现在我想要添加“第一页”和“最后一页”的链接到我的分页中,同时当搜索结果过长时也需要添加“...”。例如,我想要实现以下示例中的效果。请问谁能帮我修复代码以便我可以更新我的网站。谢谢。

First Previous 1 2 3 4 5 6 7 ... 199 200 Next Last 

我目前使用我的代码显示以下内容。
Previous 1 2 3 4 5 6 7 Next

这是我的分页代码的一部分,用于显示链接。
if ($pages > 1) {

    echo '<br /><p>';

    $current_page = ($start/$display) + 1;

    if ($current_page != 1) {
        echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
    }

    for ($i = 1; $i <= $pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
        } else {
            echo '<span>' . $i . '</span> ';
        }
    } 

    if ($current_page != $pages) {
        echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
    }

    echo '</p>';

}

你能具体说明一下你的问题是什么吗? - awgy
5个回答

1

1

0

只需获取总数并回溯:

$result_count = 200; // hypothetical

// run your regular code.....

$end_pages_to_display = 3;

for($i = $result_count; $i <= ($result_count - $end_pages_to_display); $i--)
{
    echo "<a href='index.php?page={$i}'>{$i}</a>";
}

我的意思是...这不是代码将为您的网站工作,而是完全相同的逻辑。


0

我不确定你的 's' 和 'p' $_GET 变量是用来干什么的(盐和胡椒?),所以我只处理 'p',也就是页面。

<?php
$max = '20';
if ($pages > 1) {

    echo '<br /><p>';

    $current_page = ($start/$display) + 1;

    //add this here... first will always be one
        echo '<a href="index.php?p=1">First</a>';
    if ($current_page != 1) {
        echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
    }

    for ($i = 1; $i <= $pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
        } else {
            echo '<span>' . $i . '</span> ';
        }
        // add this here... 
        if ( $i == $max){
            // stop the for() loop
            break;
        // not so fancy way of displaying last two pages, use other example if you want to get fancy.
        echo '<a href="index.php?p=' . ($pages - 1) . '">'.($pages - 1).'</a> ';
        echo '<a href="index.php?p=' . ($pages) . '">'.($pages).'</a> ';

        }
    } 

    if ($current_page != $pages) {
        echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
    }
        echo '<a href="index.php?p=1">Last</a>';
    echo '</p>';

}
?>

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接