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
  #2  
Old 07-15-2006, 03:29 AM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Phillip... you are my hero, i needed this, thanks!

:banana:
Reply With Quote
  #3  
Old 07-15-2006, 08:06 PM
Kirk Y's Avatar
Kirk Y Kirk Y is offline
 
Join Date: Apr 2005
Location: Tallahassee, Florida
Posts: 2,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Sweet! I've never taken the time to figure this out on my own, thanks Revan!
Reply With Quote
  #4  
Old 07-30-2006, 06:07 PM
King Kovifor's Avatar
King Kovifor King Kovifor is offline
 
Join Date: Nov 2004
Location: PA
Posts: 3,872
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I fixed my problems.
Reply With Quote
  #5  
Old 08-02-2006, 08:04 PM
Sean S's Avatar
Sean S Sean S is offline
 
Join Date: Jan 2004
Location: Chicago
Posts: 301
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

you are my hero x 2 . very neat tutorial Revan, thank you.

I also found something else that I thought would be a good addition for those that might run into the same problem.

The problem I faced was that if I didn't want the navbar template included in the page, I would get a javascript error and the jump to page option did not open.

So if you have a page that does not include the $navbar, put this instead of it and it should get rid of the error and also show the jump to page input box.

Code:
<if condition="$show['popups']">
<!-- PAGENAV POPUP -->

	<div class="vbmenu_popup" id="pagenav_menu" style="display:none">
		<table cellpadding="4" cellspacing="1" border="0">
		<tr>
			<td class="thead" nowrap="nowrap">$vbphrase[go_to_page]</td>
		</tr>
		<tr>
			<td class="vbmenu_option" title="nohilite">
			<form action="$vboptions[forumhome].php" method="get" onsubmit="return this.gotopage()" id="pagenav_form">
				<input type="text" class="bginput" id="pagenav_itxt" style="font-size:11px" size="4" />
				<input type="button" class="button" id="pagenav_ibtn" value="$vbphrase[go]" />
			</form>
			</td>
		</tr>
		</table>
	</div>

<!-- / PAGENAV POPUP -->
</if>
Reply With Quote
  #6  
Old 08-12-2006, 03:45 PM
Princeton's Avatar
Princeton Princeton is offline
 
Join Date: Nov 2001
Location: Vineland, NJ
Posts: 6,693
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great article ... thanks for sharing with the community
Reply With Quote
  #7  
Old 08-16-2006, 05:09 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

oh my, I am paginating everything now!!!
Reply With Quote
  #8  
Old 08-25-2006, 04:25 AM
harmor19 harmor19 is offline
 
Join Date: Apr 2005
Posts: 1,324
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you. It works beautifully!
Reply With Quote
  #9  
Old 09-24-2006, 05:11 PM
Surviver's Avatar
Surviver Surviver is offline
 
Join Date: Feb 2006
Location: Bonn, Germany
Posts: 382
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you, great Tutoril !
Reply With Quote
  #10  
Old 08-16-2007, 12:59 PM
X Quiz X Quiz is offline
 
Join Date: Apr 2004
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks, but what about in admincp? how can we paginate the results?
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:53 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.08331 seconds
  • Memory Usage 2,317KB
  • Queries Executed 23 (?)
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_code
  • (1)bbcode_html
  • (6)bbcode_php
  • (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
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)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