0

I am trying to paginate through a set of results. I know how to do a simple pagination, but I have a lot of records in the database. So the problem is that when I have a lot of pages, the number of links are also high, therefore breaking the page and making the page ugly.

What I want to accomplish is that I only want to show 7 links ...
First, 1, 2, 3, 4, 5, Last

Also I would like to centralize the current page when the user get to a higher page than 3, lets say 6 ...
The user will get ...
First, 3, 4, 5, 6, 7, 8, 9, Last

With 6 as the centralized number.

Here is the code ...

<!-- Pagination Setup -->
<?php isset($_GET["page"]) ? $current_page = $_GET["page"] : $current_page =  1; ?>
<?php $per_page = 2; $start = ($current_page - 1) * $per_page; ?>

And here is what I have come up with so far ...

<!-- If there are more results (End:1) -->
<?php if($pages > 1){ ?>
<div class="Pagination">
    <!-- If current page is not equal to 1 add 'First' button (End:2) -->
    <?php if($current_page != 1){ ?>
    <a href="index.php?page=1">First</a>
    <?php } ?>
    <!-- End:2 -->

    <!-- For loop (End:3) -->
    <?php for($x = ($current_page - 3); $x <= $pages; $x++){ ?>
        <!-- If current page is equal to page or $x disable link -->
        <?php if($x == $current_page){ echo $x; continue; } ?>

        <!-- If page or $x is smaller than 1 continue or do not show -->
        <?php if($x < 1){ continue; } ?>

        <a href="index.php?page=<?php echo $x ?>"><?php echo $x; ?></a>
    <?php } ?>
    <!-- End:3 -->

    <!-- If current page is not the last one show 'Last' link -->
    <?php if($pages != $current_page){ --$x ?>
    <a href="index.php?page=<?php echo $x ?>">Last</a>
    <?php } ?>
</div>
<?php } ?>
<!-- End:1 -->

If you try to run the script, what you will get is that it works fine but only for the previous numbers.
So in this case 3, 4, 5.
But after the 6 It doesn't filter, it shows all pages ...

Hope that the question is clear,
Farris

3
Contributors
5
Replies
51
Views
2 Years
Discussion Span
Last Post by broj1
0

Maybe this is not an answer to your question but I was just wondering why you do not use any of the ready made solutions for pagination. You can find some examples on phpclasses.org (they will want you to sign-up to download the code but I think it is worth it). The advantages are: you have a uniform class that handles pagination (in uniform way) on all pages with many database results, when you change the look of the pagination links the changes will reflect everywhere, you can add customization options through various methods etc. The bad side is that by using some else's solution you do not learn that much.

0

I am sure the code can be found on this site too. I know I've posted one possible solution, but I haven't found it yet.

0

The following is a function that will create links to pages. In addition to the First and Last buttons I also added the Next and Prev buttons which might be useful. I avoided mixing html and php code so html statemens are being echoed. The links are wrapped in a div so you can style the elements. Also links are wrapped in span elements, again for styling. The separator between links is customizable but you could get even better look using CSS.

This function works but not everything might be meeting your expectations. It is to show the concept, you fine tune it yourself.

function displayLinks($totalPages, $displayedLinks = 6, $currenttPage = 1) {

    // customizable link separator
    $separator = " | ";

    // when number of pages is less than 6 (or whatever you set it to be)
    if($totalPages < $displayedLinks) {
        $displayedLinks = $totalPages;
    }

    // calculate the first and last page links
    $startPageLink = $currenttPage <= floor($displayedLinks / 2) ? 1 : $currenttPage - floor($displayedLinks / 2);
    $endPageLink = ($currenttPage + $displayedLinks) < $totalPages ? $startPageLink + $displayedLinks : $totalPages;

    if(($totalPages - $endPageLink) < (floor($displayedLinks / 2))) {
        $startPageLink = $totalPages - $displayedLinks;
    }

    // enclose the links in a div so you can style everything
    echo '<div class="pagination-links">';

    // add the 'First' and the 'Prev' buttons if links do not start with 1
    if($startPageLink > 1) {
        echo '<a href="'. $_SERVER["SCRIPT_NAME"] . '?page=1">First</a>';
        echo $separator;
        echo '<a href="'. $_SERVER["SCRIPT_NAME"] . "?page=" . ($currenttPage - 1) . '">Prev</a>';
        echo $separator;
    }

    // echo page links
    for($p = $startPageLink; $p <= $endPageLink; $p++) {

        if($p == $currenttPage) {
            echo "<span class=\"current-page\">$p</span>";
        } else {
            echo '<span class="pagination-link"><a href="'. $_SERVER["SCRIPT_NAME"] . "?page=$p" . '">' . "$p</a></span>";
        }

        if($p < $endPageLink) {
            echo $separator;
        }
    }

    // add the 'Next' and 'Last' buttons if last displayed last link is not higher than total number of pages
    if($endPageLink < $totalPages) {
        echo $separator;
        echo '<a href="'. $_SERVER["SCRIPT_NAME"] . "?page=" . ($currenttPage + 1) . '">Next</a>';
        echo $separator;
        echo '<a href="'. $_SERVER["SCRIPT_NAME"] . "?page=$totalPages" . '">Last</a>';
    }

    // close the pagination div
    echo '</div>';
}

The way to use the function is like this:

isset($_GET["page"]) ? $current_page = intval($_GET["page"]) : $current_page =  1;
$per_page = 2;
$start = ($current_page - 1) * $per_page;
$pages = $rowCount / $per_page;
$query = "SELECT * FROM tablename LIMIT $start, $per_page");
// display resulting rows
...
// display links
$displayedLinks = 6;
displayLinks($pages, $displayedLinks, $current_page);

Still you might be better off using some of ready made solutions that has been tested and offers more functionalities.

Edited by broj1

0

I have come up with a simple solution,
Here is the code,

<!-- If there are more results (End:1) -->
<?php if($pages > 1){ ?>
<div class="Pagination">
    <!-- If current page is not equal to 1 add 'First' button (End:2) -->
    <?php if($current_page != 1){ ?>
    <a href="index.php?page=1">First</a>
    <?php } ?>
    <!-- End:2 -->

    <!-- For loop (End:3) -->
    <?php for($x = ($current_page - 3); $x <= $pages; $x++){ ?>
        <!-- If current page is equal to page or $x disable link -->
        <?php if($x == $current_page){ echo $x; continue; } ?>

        <!-- If page or $x is smaller than 1 continue or do not show -->
        <?php if($x < 1){ continue; } ?>

        <!-- Here is the line which I added -->
        <?php if($x > $current_page + 3){ continue; } ?>
        <a href="index.php?page=<?php echo $x ?>"><?php echo $x; ?></a>
    <?php } ?>
    <!-- End:3 -->

    <!-- If current page is not the last one show 'Last' link -->
    <?php if($pages != $current_page){ --$x ?>
    <a href="index.php?page=<?php echo $x ?>">Last</a>
    <?php } ?>
</div>
<?php } ?>
<!-- End:1 -->



<?php if($x > $current_page + 3){ continue; } ?>

This line did the trick.

Thank you guys for your help,
Farris

Edited by FarrisFahad

0

You are welcome. If there are no more questions please mark the thread as solved. Happy coding in 2015.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.