Results 1 to 2 of 2

Thread: [RESOLVED] pagination help

  1. #1

    Thread Starter
    Fanatic Member stickman373's Avatar
    Join Date
    Mar 2001
    Location
    MA
    Posts
    909

    [RESOLVED] pagination help

    http://www.ekc-solutions.com/propertybbs/test.php

    if you go to the page above you can see it does the pagination, but is there a way to show the first 10 page links like

    < 1 2 3 4 5 6 7 8 9 .. 90 >>

    something like that?

    Here is the code:

    Code:
    if(!isset($_GET['page'])){
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
    
    // Define the number of results per page
    $max_results = 10;
    
    // Figure out the limit for the query based
    // on the current page number.
    $from = (($page * $max_results) - $max_results);
    
    // Perform MySQL query on only the current page number's results
    
    $sql = mysql_query($totalquery." LIMIT $from, $max_results");
    
    while($row = mysql_fetch_array($sql)){
        // Build your formatted results here.
        echo $row['title']."<br />";
    }
    
    // Figure out the total number of results in DB:
    $total = mysql_query($totalquery);
    $total_results = mysql_num_rows($total);
    
    // Figure out the total number of pages. Always round up using ceil()
    $total_pages = ceil($total_results / $max_results);
    
    // Build Page Number Hyperlinks
    echo "<center>Select a Page<br />";
    
    // Build Previous Link
    if($page > 1){
        $prev = ($page - 1);
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a>&nbsp;";
    }
    
    
    
    for($i = 1; $i <= $total_pages; $i++){
        if(($page) == $i){
            echo "$i&nbsp;";
            } else {
                echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>&nbsp;";
        }
    }
    
    // Build Next Link
    if($page < $total_pages){
        $next = ($page + 1);
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
    }
    echo "</center>";
    Last edited by stickman373; Dec 11th, 2004 at 02:24 PM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: pagination help

    Yes there is. Pagination is a whole subject in its self, as long as you know exactly what you need it shouldn't be too hard. Personally I like them to look like this:


    << First < Previous 2 3 4 5 6 7 8 9 10 11 12 Next > Last >>

    These are the things you need to know before you code:
    • How many items are going to be listed per page?
    • How many additional pages are you going to provide links for?
    • How will you display those links?


    It is best to separate the generation of the links and the extraction of the data from the database into two different functions. Heres a link to a thread The Hobo posted about pagination, its worth a look, because he has quite a neat function to generate the links. If you intend on using it, make sure you PM him to get his permission

    http://www.vbforums.com/showthread.php?t=298755

    As for extracting the data, you need the following pieces of information:
    • How many rows to extract (i.e: number of items per page)
    • Which item to start at. (i.e: number of items per page multiplied by the current page number)


    In mysql you can use the LIMIT clause to select a subset of items from a select query (remembering that row numbers are zero based).
    Code:
    SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT 9,10;
    The following query will get rows 10 to 19 from the table. The SQL_CALC_FOUND rows, causes the server to execute the entire query in the background and store the total number of rows. This is necessary so you can calculate how many pages there are going to be. To find out how many rows just execute this query imeadiatley after:
    Code:
    SELECT FOUND_ROWS();
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width