[HOW TO - vB4] Paginating Results
Introduction
Not very much has changed regarding how pagination works since vB 3.8, but quite enough to warrant an update of Revan's great tutorial, dating from 2006. Kudos to Revan. As Revan I assume you know your way around php and can understand at least roughfly what each bit of the code does. There'll be explanations, of course, but this is for coders and mod developers. The main work for pagination has to be done in the PHP-code, still no surprise there. Cleaning URL parameters We start off by cleaning the URL parameters that will tell our paginator how many entries to show per page and on which page of the resultset we're actually on PHP Code:
Counting results The next step is to count the number of resuts that our actual database query will return (this query we will meet later on - it's an example that will throw all users with no posts). PHP Code:
Settings & sanitizing Next we're off to do some settings: The first argument is our counting result. The last two arguments are the maximum number of results per page (100) and the default number of results per page (20). Note how the first argument is the result of the count query above. You can replace those with settings from the AdminCP by inserting the corresponding variables obviously. PHP Code:
Orientation - where are we? Now some stuff to determine on which page we're on and which results to show. Note how you have to provide the results of our initial count query: PHP Code:
The main query Now the query that throws all users with no posts - just an example, obviously. Note the LIMIT - this has to be added to the original query to delimit the resultset for the actual page PHP Code:
Constructing pagenav The last (but one) thing to do in the PHP code is to finally call the function to construct the actual page-nav and saving it's output to a variable for passing to the template.
PHP Code:
Registering pagenav for templates As with all variables in vB4, the newly created $pagenav has to be registered to be used inside the template: PHP Code:
Template code Now you just have to put the pagenav-code into your template, and we're done. Note that the id of the surrounding div should be changed to "pagination_bottom" or "pagination_top" accordingly. Code:
<vb:if condition="$pagenav"> -c As of now (beta 3), there are still some issues that need to be resolved regarding pagination. They are minor and there should not be substantial changes, but be aware of that. For example, the setting of results shown per page does not work yet - 20 is hardcoded into the sanitizing function. |
How to get this to work with $_POST.
Every time I click a link in the pagination it resets the post variables. When I use $_GET it shows either the security token or the session hash. Thanks for any help. |
The pagination system, as far as I know, is designed to work with URL parameters only. If you want to preserve data entered via a form in post mode, you need to pass the data entered to the pagination class by adding parameters to the pagenav link, then you need to query it accordingly when the page gets reloaded upon page change. Anyway, wihtout seeing your code it's hard to say where you are going wrong.
|
here ya go http://pastebin.com/m5dd777d5
If I use $_get how do I remove the sessionhash or the securitytoken out of the url? It loads the first page but each page after that has no variables. |
First, whenever you use get or post variables, you should always use the vbulletin input cleaner to make them safe. If you don't know how, there's an article somewhere here.
Second, why would you want to get rid of the sessionhash? It's only showing if you have cookies disabled, and if you remove it, it will break login for people w/o cookies. Third, as I said, when you change pages, you basically reload them. Post variables won't survive that. You need to save the get parameters into variables, pass those variables as parameters to the pagination URL, and then read them from the URL parameters upon reload. Basically (forgoing input cleaner here, for simplicity: PHP Code:
|
I don't want to get rid of the session hash or security token I just want to hide it from the URL with $_GET for the simple fact is I don't want my users passing their sessionhash or security token around to each other.
On this search page only would it be safe to turn off CSRF_PROTECTION as long as I clean what can be inputed? |
Visibility of the security token is not a problem securitywise. It is visible in the source code anyway. The sessionhash is passed along via URL by vB if cookies are disabled, that's the way it is. And no, disabling CSRF protection is not a good idea, it's there for a reason.
|
Thanks so much for all your help man.
Last question do I need to add the following to $_GET forms? Code:
<input type="hidden" name="securitytoken" value="$bbuserinfo[securitytoken]" /> |
If you're using GET, you don't need the security token, but you do need the session (as I said, leaving that out will break log in for everyone with cookies disabled).
On CSRF protection, you may want to read https://vborg.vbsupport.ru/showthread.php?t=177013 |
Again Thanks for all the help Great Article.
Most of my problem boiled down to the sql statment. I had Code:
SELECT DISTINCT COUNT(blah Code:
SELECT COUNT(DISTINCT blah I used this same tutorial on several other pages of mine and it went flawlessly. Thanks again for the fast answers and the great help. P.S. I secure my code after its working just to narrow down problems. |
I would really appreciate it if somebody could help me out.
I've built a page that pulls row from a sql db. Now I need to paginate the page as there are too many rows for one page. I've followed the instructions as far as I can, changing only a few things, such as the sql queries. The php is now limiting the number of rows to the correct perpage number. I've added the pagenav code to my custom template, but no matter what I do, the pagenav doesn't show. Any help would be greatly appreciated. S1OPP |
Quote:
|
^^What he said :)
Quote:
|
no problem.
sightings.php PHP Code:
PHP Code:
|
Worked it out now,
thanks. |
Sorry, didn't come around to look at your code - but you working it out yourself is much better anyway :)
|
Dont forget to use the & like this when passing additional attributes. :)
PHP Code:
This one seems to work at first but it does not when using the page number input: PHP Code:
|
As of VB 4.0.3, I now have errors with page pagination on custom mods...
Example: http://test.8wayrun.com/media/c9-kogarasumaru With VB 4.0.2, it looked fine, but now the last 3 elements are shifted. |
Only happens in Chrome... only with vb4.0.3
https://vborg.vbsupport.ru/attachmen...hmentid=116129 This is code... Code:
<vb:if condition="$pagenav"> |
Just wanted to say thank you for this article on pagination, I was finally able to understand it thanks to your post. Here is my example for those who are still trying to understand maybe this helps.
PHP Code:
|
Hello,
Any idea why this method even working fine in other (custom) pages, does not works in member.php? I've added a custom tab there (thank you again as that aricle is also yours), I used the same code that I had in my php file (copy & paste), but even if the navbar appears, it has:
Thank you C.T. |
Just to post a followup. Finally I was able to find what causes the error, but now it left the harder, on how to solve it.
vBulletin when constructs a page adds page=xx at the end of link. The problem is when the link contains &tab=mytab#selectedtab. In this case seems that vB ignores anything after &tab=mytab#selectedtab, so it can't get the inbound page nbr. I tried placing &page=2 before &tab=mytab#selectedtab, and it works fine. Now I need to find a way to get the actual pagenumber to set it there as variable. $pagenumber does not works as it shows the same page. C.T. |
When you construct the link, you can pass an anchor. The selected tab should be present as a variable ($selected_tab?), and since the anchor is always the same as the id, you should be able to completely construct the URL needed.
|
Quote:
Code:
$pagenav = construct_page_nav($pagenumber, $perpage, $records, 'member.php?' . $vbulletin->session->vars['sessionurl'] . 'u='.$userid.'&action=1&tab=classifieds#classifieds'); Code:
$pagenav = construct_page_nav($pagenumber, $perpage, $records, 'member.php?' . $vbulletin->session->vars['sessionurl'] . 'u='.$userid.'&action=1', '&tab=classifieds#classifieds'); |
Hello everyone, quick question;
What about multiple pagination instances on the same template for example? I am having a bit of trouble with the second and third instances. I created 3 separate files and inserted them into the same template as plugins. Using three instances of: HTML Code:
<vb:if condition="$pagenav"> HTML Code:
<div id="test" class="block collapse"> |
As far as I can see, the vB pagination classes won't work for multible instances per page. For what you seem to be after, I'd use some nifty jQuery AJAX thingy, to be honest, to get around all the page reloads the php method causes.
|
Quote:
|
I run my script like this myscript.php? id = $ id how I could add
PHP Code:
how to add $ id in this part of code: PHP Code:
|
Glad I found this article... Still something new to learn. :)
|
trying to implement this on an admin page.
|
Won't work. AdminCP pages work completely different.
|
the main query works, but construct_page_nav doesnt, looking into just using nextpage for admin pages.
edits.... https://vborg.vbsupport.ru/showthread.php?t=200413 I used this code for the admincp results. the code in this thread works great for normal pages tho. |
All times are GMT. The time now is 10:26 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|