03_05-Setting_Up_Pagination

  • 11 năm trước
Transcript
00:00The account I am using for this project has only a handful of friends, and in reality people
00:07So we need to learn how to take large amounts of data and divide it into pages.
00:13So limiting the number of results is actually quite easy.
00:22I am going to make it a little more official.
00:28want to ask from the open graph at a time. So that's going to go up here.
00:37I don't have that many users.
00:43this account, so this will work a little bit better.
00:52a limit of my quantity here.
01:04So now I am only getting two users, because some users haven't liked any movies so they don't appear here.
01:12It's not a big deal.
01:17So if I want to see the rest of the info, I need to separate my requests into pages,
01:24First, I need to find out how many friends the current user has.
01:31The moviespath is now being limited by our quantity.
01:41So I can take that to make a call that returns the number of friends.
01:50how many friends and use PHP's count method which counts the number of items in an array
02:05Remember that when you query the Open Graph, if you take a look at the Open Graph API,
02:14going to be in a data array.
02:21Let's go ahead and print out the number of friends we have on this account.
02:29Right on top of this I'll add an echo command, and it's going to print out the number of friends this user has.
02:43sure that I put that this is part of the paging area.
02:54and at the bottom of the application it should say that I have eight friends.
03:03Now that we have both the quantity of users we want to use per page and the total number
03:14and the offset we need for the next page.
03:24I am going to set it to $howmanyfriends divided by the quantity or the amount of friends we
03:35So total page is going to be the amount of friends we have divided by the $qty we want to show per page.
03:45doesn't divide properly. So ceil is going to round the number up.
03:537 divided by 3 is 2.33, and we don't want to just round the number, because that would give us 2.
04:04Even if that last page only has one result.
04:12of this $howmanyfriends, and I'll output the variable $totalpages here.
04:22that we have everything typed in perfectly.
04:27You don't want to go too far before you test to see that everything is working properly.
04:38Sometimes you will see this kind of weird scrolling.
04:45You can refresh the page.
04:50So it's correctly saying that since I am dividing my pages three users at a time, this application
05:02we are going to need to apply an offset to our Open Graph path.
05:09with a variable called offset.
05:17So I am going to go all the way to the top of my file, and right after I get this quantity
05:28$_GET and get a value from URL called offset.
05:44Now that I have this, I am going to create a couple of other variables.
05:56That's going to be the value of the currentoffset divided by the amount of users I want to display per page.
06:07And then finally, I am going to get the value of the $nextoffset,
06:19I'll add some comments here.
06:27I am just going to copy this line, paste it a couple times,
06:35if I have done everything correctly.
06:47So we'll save that.
06:51And if we scroll it to the bottom, we should see all that information.
06:59This is page number one, and the next offset will start after user number three.
07:09I am not going to need them in my final application, I will refresh.
07:16I just want to have how many friends the user has and then I want to go back to my page.
07:26how you want to offset each call to the Open Graph API, and based on that you can set up
07:36offset of the next page.