PHP& MySQL分页更新帮助

时间:2010-05-10 23:30:43

标签: php mysql

自从我在网页上更新了我的分页后,我试图在搜索时添加FirstLast链接以及搜索时...结果很长。例如,我正在尝试在下面的示例中实现以下内容。有人可以帮我修复我的代码,以便我可以更新我的网站。感谢

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

}

5 个答案:

答案 0 :(得分:1)

为了不重新发明轮子,你可能想看看这个简单的PHP分页类:

http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/

它可能不适合你开箱即用,但它很容易修改并重新编码比分页更有趣的事情:)

答案 1 :(得分:1)

您需要的唯一教程。 http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

它对我有用。该教程还有第2部分,但您不需要它。它告诉您如何修改现有代码。

答案 2 :(得分: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>";
}

我的意思是......这不是代码适用于您的网站,但它是完全相同的逻辑。

答案 3 :(得分:0)

我不确定你的'''和'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>';

}
?>

答案 4 :(得分:0)

可能有一种更简单的方法:使用预先构建的库......

http://framework.zend.com/manual/en/zend.paginator.html

相关问题