Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
[How-To] Paginate your results
Revan's Avatar
Revan
Join Date: Jan 2004
Posts: 1,671

Started doing my first if...else chain in 2004, and released my first major vBulletin modification in August 2004 with the first version of the RPG Integration Hack.

Norway
Show Printable Version Email this Page Subscription
Revan Revan is offline 07-06-2006, 10:00 PM

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.
Reply With Quote
  #12  
Old 08-17-2007, 04:58 AM
Mixtoon's Avatar
Mixtoon Mixtoon is offline
 
Join Date: Aug 2007
Location: United Arab Emirates
Posts: 10
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by X Quiz View Post
thanks, but what about in admincp? how can we paginate the results?
I'm asking the same question!
Reply With Quote
  #13  
Old 09-13-2007, 10:27 AM
Davidinh Davidinh is offline
 
Join Date: Apr 2005
Posts: 47
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

how to Paginate your results in AdminCP page

thanks,
Reply With Quote
  #14  
Old 03-27-2008, 06:01 PM
TonyLuong TonyLuong is offline
 
Join Date: Nov 2006
Posts: 6
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I use this How To exact the way you layout and I got the Navigation to show up; I can Navigate through pages; however, I notice the IE status bar has error message "done, but with errors on page". I click on it to view the error detail, it is "'null' is null or not an object". If I comment out the construct_page_nav function call then the page disply just 1 page without the Navigation and no error produce; this makes me think the error comes from the Navigation.

Also I notice the end of has an arrow down image; when click on the image it suppose to show up a box says "Go to page...", you would then enter a page number and click "go" to go to that page; however, the arrow down on mine is not clickable; I click on it and nothing happens.

Anyone know what I might have done wrong or missing?
Reply With Quote
  #15  
Old 01-13-2010, 12:35 AM
businessmeet businessmeet is offline
 
Join Date: May 2009
Posts: 42
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Edit: Sorry, I forgot I clicked the link from this page originally:
https://vborg.vbsupport.ru/showthread.php?t=221670 brought me here. LOL! *Forehead slap moment*
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:42 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04143 seconds
  • Memory Usage 2,265KB
  • Queries Executed 19 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)bbcode_html
  • (6)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (4)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete