Results 1 to 3 of 3

Thread: Pagination Tutorial?

  1. #1

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Posts
    2

    Pagination Tutorial?

    Does anyone have a php next previous button tutorial they are willing to post?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Pagination Tutorial?

    for what? browsing through databases?

    this is what I usually use, not exactly a tutorial, but it will show you what works... I basically took it right out of an application I've already developed, so I kept in some the table data. I added a lot of comments for ease of reading, too.

    PHP Code:
    <pre>
    <?
      mysql_connect("hostname", "username", "password");
      mysql_select_db("database_name");
      $mysql['table'] = "table_name";

      $pages = array();
      //this is how many records we display per page
      $pages['display'] = 25;
      //this is the current page that we're on, defined by GET['pg']
      $pages['current'] = (isset($_GET['pg']) && is_numeric($_GET['pg']) && $_GET['pg'] > 0) ? $_GET['pg'] : 1;
      //this is the number we start at in our query
      $pages['cur_nums'] = (($pages['current'] - 1) * $pages['display']);
      //this is what we append to our query to limit the results
      $pages['query'] = " LIMIT " . $pages['cur_nums'] . ", " . $pages['display'];
      $sql = "SELECT " . 
             "id, displayname, wins, losses FROM {$mysql['table']} " .
             "WHERE ratingh>0 AND activation='' " . 
             "ORDER BY rating DESC";
      //now query the database twice, once to get the results for this page
      $query = mysql_query($sql . $pages['query']);
      //and secondly to get the TOTAL results from the database
      //  note: just for the number, not for the data
      $count = mysql_query($sql);

      //find out if we have any results
      $n = mysql_num_rows($count);
      if($n == 0 || mysql_num_rows($query) == 0){
        echo 'No information!';
        $nodisplay = true;
      }
      //do we need to display pages?
      if($n > $pages['display']){
        //this is how many pages we need to have
        $pages['pages'] = ceil($n / $pages['display']);

        //we do this so that it will display (1 to 25) and not (0 to 25)
        $c = ($pages['cur_nums'] == 0) ? 1 : $pages['cur_nums'];

        //this is the last number on the current page
        $pages['this'] = ($pages['cur_nums'] + $pages['display']);
        if($pages['this'] > $n) $pages['this'] = $n;

        //this is the complex version of what we display to the user
        //  ie: ("displaying 1 to 25 of 200 results")
        $c .= "</strong> to <strong>" . $pages['this'] . "</strong> of <strong>" . $n;
      }else{
        //this is the simple version of what we display to the user
        //  ie: ("displaying 21 results")
        $c = $n;
      }
      //define the first index as correct number
      //  (ie: page 2 starts at 26 and goes to 50, while page 1 would start at 1 and go to 25)
      $i = $pages['cur_nums'];
      //if there were results, we display them now
      if($nodisplay == false){
    ?>
    displaying <strong><?php echo $c?></strong> results
    <div style="border-top: 2px solid #000; border-bottom: 2px solid #000; ">
    <?php
        
    while($array mysql_fetch_array($query)){
          
    $i++;
          echo 
    "  #" $i ". " $array['id'] . " : " $array['displayname'] . " " $array['wins'] . "-" $array['losses'] . ", (" floor(($array['wins'] / ($array['wins'] + $array['losses'])) * 100) . "% wins) \n";
        }
    ?>
    </div>
    <?php
        
    //display our page numbers
        
    if(isset($pages['pages'])){
          for(
    $i 1$i < ($pages['pages'] + 1); $i++){
            if(
    $pages['current'] == $i) echo '<strong>';
            echo 
    '<a href="./pages.php?pg=' $i '">' $i '</a> ';
            if(
    $pages['current'] == $i) echo '</strong>';
          }
        }
      }
    ?>
    </pre>
    a working example of this exact script (no changes AT ALL, except for the database/password and stuff, so this is live information!) can be found right here. the information is from a ladder system, and is sorted by their rating (which essentially defines their rank, which is $i).

    hope that helps! feel free to ask questions or, if someone else reads through this, feel free to tell me my system sucks?!@
    Last edited by kows; Dec 22nd, 2006 at 12:06 PM.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    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