开发者

PHP & MySQL Pagination Update Help

Its been a while since I updated my pagination on my web page and I'm trying to add First and Last Links to my pagination as well as the ... when the search results are to long. For example I'm trying to achieve the following in the example below. Can some one help me fix my code so I can update my site. Thanks

First Previous 1 2 3 4 5 6 7 .开发者_开发知识库.. 199 200 Next Last 

I currently have the following displayed using my code.

Previous 1 2 3 4 5 6 7 Next

Here is the part of my pagination code that displays the links.

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

}


In the interest of not reinventing the wheel, you may want to check out this simple PHP pagination class:

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

It probably won't be perfect for you out of the box, but it's pretty easy to modify and get back to coding things a lot more interesting than pagination :)


The only tutorial you need. http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

It worked for me. There is also a part 2 to that tutorial but you won't need it. It tells you how to modify your existing code.


Just get the total and backtrack:

$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>";
}

I mean... it's not the code that's going to work for your site, but it's the exact same logic.


I am not sure what your 's' and 'p' $_GET variables are for (salt and pepper?), so i am just working with the 'p', for page.

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

}
?>


There might be an easier way: use a pre-built library...

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜