...

View Full Version : Making a Good Paginated Page



Joseph Witchard
01-12-2010, 04:58 AM
I'm beginning to look at my coding methods and wonder if I could have done the same thing better and more efficiently. Since I had such good responses with my previous question about the search form, I'm going to try my luck again.

My homepage prints out news items from MySQL, kind of like a blog. Does anyone know of a good way to paginate the results? A code example you could give me, for example?

oesxyl
01-12-2010, 05:10 AM
I'm beginning to look at my coding methods and wonder if I could have done the same thing better and more efficiently. Since I had such good responses with my previous question about the search form, I'm going to try my luck again.

My homepage prints out news items from MySQL, kind of like a blog. Does anyone know of a good way to paginate the results? A code example you could give me, for example?
there are few premade scripts but in case you want to do your own, the recipe is simple, :)
your mysql query will ends in ' limit $offset, $rows_per_page' and you need some php code to compute the current page, offset, links to each page, format the query, .....

best regards

Len Whistler
01-12-2010, 11:14 AM
This is pagination at it's simplest form. It requires two mysql queries. The code will need fine tuning.

index.php

<?php

$page = $_GET['page'];

$db_rows = 324; // mysql query
$results_per_page = 15;
$total_pages = ceil($db_rows / $results_per_page);
$from_db_row = ($page - 1) * $results_per_page; //mysql query

echo "Displaying $results_per_page records from db row $from_db_row<br><br>";

$page = 1;
while ($total_pages >= $page ) {
echo "<a href=\"index.php?page=$page\"> $page </a>";
$page++;
}

?>

JAY6390
01-12-2010, 11:21 AM
Take a look at my php pagination class (http://www.jaygilford.com/php/completely-customisable-php-pagination-class/). It's really simple to use. If you need any help with integrating it just let me know. Best of all you don't need to do math calculations for the query, or add limits to your query. It does it all for you. All you need to do is set the query and the page number parameter and run the paginate method and it runs it for you. You can configure the whole output too for the links using the templates

Joseph Witchard
01-13-2010, 04:54 AM
Some places display something like the page you're on, a couple of pages (links) that came before your page, a couple of pages (links) that come after the page you're currently on, and then ... and the very last pages with any results on them. How do you achieve something like that?

Len Whistler
01-13-2010, 06:08 AM
Some places display something like the page you're on, a couple of pages (links) that came before your page, a couple of pages (links) that come after the page you're currently on, and then ... and the very last pages with any results on them. How do you achieve something like that?


You would add if statements within the while loop to format the results. Since you already know the total number of pages and the current page you can use basic math to only display links that match a pattern. Below is an example using the code I posted before.


It's not fully tested and doesn't produce the results I intended, but it's a starting point.


while ($total_pages >= $page ) {

if ($page == $current_page) {
echo "<b>$page</b>"; // Display current page as bold text, not a link
}

$last_two_pages = $total_pages - 2;
$page_difference = $current_page - $page;


// Display links for first two pages, last two pages and 3 pages within current page.
if ($page_difference <= 3 || $page <= 2 || $page >= $last_two_pages) {
echo "<a href=\"index.php?page=$page\"> $page </a>";
}

$page++;

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum