Back to vBulletin 3 Articles

[How-To] Paginate your results
by Revan 07 Jul 2006
Rating: (1 vote - 5.00 average)

Don't you just hate it...? You have hundreds, no maybe even thousands of records being returned from a query, and the page lags for seconds at a time as well as the page being longer than a giraffe's neck.
Wouldn't it be neat to seperate them into pages? Just like vBulletin does? Of course it would.

This guide assumes you are familiar enough with vBulletin and PHP in general, so I won't go into great detail about what each bit does.
This is intended for hack authors.

So, without further ado, I give you (drumroll please) [How-To] Paginate your results!
  1. Your PHP code
    1. The first thing you need to do is clean the two variables that tells you what page you are on and how many records to clean per page.
      Add this code:
      PHP Code:
          // Default page variables
          
      $perpage $vbulletin->input->clean_gpc('r''perpage'TYPE_UINT);
          
      $pagenumber $vbulletin->input->clean_gpc('r''pagenumber'TYPE_UINT); 
    2. Then, you have to count all the entries that exists in the table you wish to display results from. Like so:
      PHP Code:
          // Count all log entries
          
      $bannedcount $db->query_first("
              SELECT COUNT(`uid`) AS `bannedcount`
              FROM `" 
      TABLE_PREFIX "lin2banlist`
          "
      ); 
    3. Next up, you'll want to call a fancy vB function that'll make sure all these variables play nice together.
      PHP Code:
          // Make sure all these variables are cool
          
      sanitize_pageresults($bannedcount['bannedcount'], $pagenumber$perpage10025); 
      The last two arguments, in this example 100 and 25, are the Max Per Page and Default Per Page.
      You may want to swap them with vBOptions settings you write, or you can choose your own values.
    4. Next up, we are going to prepare your variables for the SQL query. This we do by using this code I blatantly copied from a random vB file:
      PHP Code:
          // Default lower and upper limit variables
          
      $limitlower = ($pagenumber 1) * $perpage 1;
          
      $limitupper $pagenumber $perpage;
          if (
      $limitupper $bannedcount['bannedcount'])
          {
              
      // Too many for upper limit
              
      $limitupper $bannedcount['bannedcount'];
              if (
      $limitlower $bannedcount['bannedcount'])
              {
                  
      // Too many for lower limit
                  
      $limitlower $bannedcount['bannedcount'] - $perpage;
              }
          }
          if (
      $limitlower <= 0)
          {
              
      // Can't have negative or null lower limit
              
      $limitlower 1;
          } 
      The only bit you have to replace here is the $bannecount['bannedcount'] which is the variable we fetched in the count query above.
    5. Then we have the actual query. An example is this:
      PHP Code:
          $bannedusers_q $db->query_read("
              SELECT
                  `lin2banlist`.*,
                  `user`.`username` AS `bannedby`
              FROM `" 
      TABLE_PREFIX "lin2banlist` AS `lin2banlist`
              LEFT JOIN `" 
      TABLE_PREFIX "user` AS `user` ON(`user`.`userid` = `lin2banlist`.`bannerid`)
              LIMIT " 
      . ($limitlower 1) . ", $perpage
          "
      ); 
      The only bit about the query you need to blatantly steal is the LIMIT clause. That's what's ensuring the pages are splitted like you want them to be. I just copied it all because I wanted to.
    6. The comment above this bit of code makes it self-explanatory:
      PHP Code:
          // Finally construct the page nav
          
      $pagenav construct_page_nav($pagenumber$perpage$bannedcount['bannedcount'], 'l2cp.php?' $vbulletin->session->vars['sessionurl'] . 'do=viewbanned'); 
      Obviously you will want to replace the filename and do= section as well as the $bannedcount variable.
  2. Your template code
    Put this code above and below your content table to generate a pagenav on top and bottom:
    HTML Code:
    <table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin-bottom:3px">
    <tr valign="bottom">
        <td class="smallfont">&nbsp;</td>
        <if condition="$pagenav"><td align="$stylevar[right]">$pagenav</td></if>
    </tr>
    </table>

And you're done
If everything works correctly you should now have a fully vBised page navigation.

vblts.ru supports vBulletin®, 2022-2024