Home
Username:
Password:
Facebook Application Development Tutorials

Displaying Data / Paginating Results pt. 2




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Now here's the code in paginate that does all the work. Remember you saw that the limit keyword that can be appended to the sql queries can have two values after it. Most people just use limit to limit the number of records returned so they say something like limit 25 at the end of a query and they get at most 25 records. But there can actually be two parameters after it and the first one is the zero relative record number to start so if you type in at the end of a query limit0, 25 you'll get at most 25 records starting from the zeroth record, so at most you will get record zero through 24, using sql's zero relative numbering. So here is where we're going to prepare, in pagination, some conversions between the zero relative numbering that we're going to have to pass into the sql queries and the one relative numbering that is usable for human reading. And we start up here. If we have a baseURL which is passed in all the way from the initial call to get from database and render and that means that we want a callback, we want pagination and we're also passing in the rowCount, the number of rows that will be shown on each page. So here, if we want pagination, and the total number of rows is greater than the rowCount then I'm in the world of pagination. If the total number of rows retrieved is less than or equal to the rowCount I don't want any pagination and you've already seen, the pagination controls disappear when they're irrelevant, so I'm only here when I want pagination and I need it. And here, what I'm doing is I'm setting the previous offset and it's commented here, so we can see that we're zero relative. We're setting the offset for the next batch and we also are constructing the information of, in a variable, the number of rows on the last page so we can see how many there will be there. Now we come into the section where we set the 1 relative row record numbers for the user interface displaying records 1 to 4 or whatever and we're coming here, we're taking the offset, which is zero relative and we're making it into a 1 relative. Then we do a little bit more manipulation to get the previous and the next page numbers. Up here, remember, we are record numbers, row or record numbers, now here we're in the world of page numbers, which are always 1 relative and they are obviously in most cases more than one row or record on a page. And here is how pagination works. If we need links we are setting up links here to the baseURL that was passed in and these are the first and previous page numbers and the last and next page numbers constructed here from those parameters that have been unloaded and that also have been retrieved from the database. Remember, the first time through get from database and render I did a count to find out how many records there were, so that is how all of this information has been constructed. You don't have to do anything with it now. This is the code in pagination and paginate does it and as long as you are passing in the correct values, all the way from the first call to get from database and render and all you really have to do is say where you're starting from and the number of items on a page, paginate will work. And then you come down here and this has constructed the links for the first, previous and next page and so forth, at the top of the page. It's also said we're displaying this record to that record out of the total number of rows and these are one relative; next and previous page numbers. Here is the code that is calling the rendering routine and if we want a footer with the same pagination controls in the footer we've got all the data and we just emit the code. So, that is what you need in paginate and in fact, in paginate you may not have to change any code at all in the paginate function unless you want to change the way in which it's displayed, but this is the code that you will need to go down and then you may not even need to change this code because all you have to do is process the data and you just need to pass in the name of the function that it's going to be called and most people will just leave it as it is. So paginate, which is the most complicated function here, may not require any changes at all.

Tutorial Information

Course: Facebook Application Development
Author: Jesse Feiler
SKU: 34058
ISBN: 1-935320-81-5
Release Date: 2009-11-23
Duration: 7.5 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available