Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts

    Making a Good Paginated Page

    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?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Joseph Witchard View Post
    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

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    This is pagination at it's simplest form. It requires two mysql queries. The code will need fine tuning.

    index.php
    PHP Code:
    <?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++;
    }

    ?>
    Last edited by Len Whistler; 01-12-2010 at 11:17 AM.
    Leonard Whistler

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Take a look at my 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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by Joseph Witchard View Post
    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.

    PHP Code:
    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 <= || $page <= || $page >= $last_two_pages) {
    echo 
    "<a href=\"index.php?page=$page\"> $page </a>";
    }

    $page++;


    Leonard Whistler


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •