PDA

View Full Version : [How-To] Paginate your results


Revan
07 Jul 2006, 11:44
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!
Your PHP code
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:
// Default page variables
$perpage = $vbulletin->input->clean_gpc('r', 'perpage', TYPE_UINT);
$pagenumber = $vbulletin->input->clean_gpc('r', 'pagenumber', TYPE_UINT);
Then, you have to count all the entries that exists in the table you wish to display results from. Like so:
// Count all log entries
$bannedcount = $db->query_first("
SELECT COUNT(`uid`) AS `bannedcount`
FROM `" . TABLE_PREFIX . "lin2banlist`
");
Next up, you'll want to call a fancy vB function that'll make sure all these variables play nice together.
// Make sure all these variables are cool
sanitize_pageresults($bannedcount['bannedcount'], $pagenumber, $perpage, 100, 25);
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.
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:
// 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.
Then we have the actual query. An example is this:
$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.
The comment above this bit of code makes it self-explanatory:
// 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.
Your template code
Put this code above and below your content table to generate a pagenav on top and bottom:
<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.

Antivirus
15 Jul 2006, 05:29
Phillip... you are my hero, i needed this, thanks!

:banana:

Kirk Y
15 Jul 2006, 22:06
Sweet! I've never taken the time to figure this out on my own, thanks Revan!

King Kovifor
30 Jul 2006, 20:07
I fixed my problems.

Sean S
02 Aug 2006, 22:04
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.


<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>

Princeton
12 Aug 2006, 17:45
Great article ... thanks for sharing with the community :)

Antivirus
16 Aug 2006, 19:09
oh my, I am paginating everything now!!! :)

harmor19
25 Aug 2006, 06:25
Thank you. It works beautifully!

Surviver
24 Sep 2006, 19:11
Thank you, great Tutoril !

X Quiz
16 Aug 2007, 14:59
thanks, but what about in admincp? how can we paginate the results?

Mixtoon
17 Aug 2007, 06:58
thanks, but what about in admincp? how can we paginate the results?

I'm asking the same question!

Davidinh
13 Sep 2007, 12:27
how to Paginate your results in AdminCP page

thanks,

TonyLuong
27 Mar 2008, 20:01
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?

businessmeet
13 Jan 2010, 02:35
Edit: Sorry, I forgot I clicked the link from this page originally:
http://www.vbulletin.org/forum/showthread.php?t=221670 brought me here. LOL! *Forehead slap moment*