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