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 ...
There might be an easier way: use a pre-built library...
http://framework.zend.com/manual/en/zend.paginator.html
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>';
}
?>
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.