Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Setting up pagination

From: Building Facebook Applications with PHP and MySQL

Video: Setting up pagination

The account I am using for this project has only a handful of friends, and in reality people can have hundreds of relationships on Facebook. So we need to learn how to take large amounts of data and divide it into pages. So let's take a look. So limiting the number of results is actually quite easy. All you need to do is add the keyword limit to the end of your path and run your app again. I am going to make it a little more official. I am going to create a variable called quantity and assign it a value of how many users I want to ask from the open graph at a time. So that's going to go up here.

Setting up pagination

The account I am using for this project has only a handful of friends, and in reality people can have hundreds of relationships on Facebook. So we need to learn how to take large amounts of data and divide it into pages. So let's take a look. So limiting the number of results is actually quite easy. All you need to do is add the keyword limit to the end of your path and run your app again. I am going to make it a little more official. I am going to create a variable called quantity and assign it a value of how many users I want to ask from the open graph at a time. So that's going to go up here.

I'll just create a variable called qty and give it a number of 3. I don't have that many users. So you can make that number bigger if you want to, but I have about eight friends in this account, so this will work a little bit better. Now I am going to go to my path which is right here, the moviespath, and I'm going to add a limit of my quantity here. Let me go ahead and save that, and I'll refresh this app, and now we should only get a few accounts.

So now I am only getting two users, because some users haven't liked any movies so they don't appear here. So that's probably going to happen to your application. It's not a big deal. Just remember that you may not get three results even if you ask for three users. So if I want to see the rest of the info, I need to separate my requests into pages, and I need a few things for that. First, I need to find out how many friends the current user has. To get that information, I can't use the moviespath. The moviespath is now being limited by our quantity.

So luckily I have another path called the user_ graph that I'm not really using in this application. So I can take that to make a call that returns the number of friends. So right after this call right here to this user_graph I can create a variable called how many friends and use PHP's count method which counts the number of items in an array to pull the user graph and the data object inside that user_graph. Remember that when you query the Open Graph, if you take a look at the Open Graph API, and you get the user's friends, you're going to get all the user friends info, and it's going to be in a data array.

So we have to go into the data array, not just the user_graph object. Let's go ahead and print out the number of friends we have on this account. I am going to scroll down. Right on top of this I'll add an echo command, and it's going to print out the number of friends this user has. I am also going to modify this div, because it doesn't have a comment and just to make sure that I put that this is part of the paging area. So I am going to save that, switch over to my application, and refresh my screen, and at the bottom of the application it should say that I have eight friends.

Obviously, yours will be different, but this account only has eight friends, and that's perfect. Now that we have both the quantity of users we want to use per page and the total number of users, we can calculate the total number of pages, the number of the current page, and the offset we need for the next page. So right after this line right here I am going to add a new variable called totalpages, and I am going to set it to $howmanyfriends divided by the quantity or the amount of friends we want to display per page.

So total page is going to be the amount of friends we have divided by the $qty we want to show per page. We are using the ceil method here, because this number can have a remainder if everything doesn't divide properly. So ceil is going to round the number up. So in other words, if we have seven friends, and we want to ask for three users at a time, 7 divided by 3 is 2.33, and we don't want to just round the number, because that would give us 2. If there is more than two pages in reality we need three pages. Even if that last page only has one result.

So let me just make sure I did everything correctly, and I am going to just make a copy of this $howmanyfriends, and I'll output the variable $totalpages here. Even though we are not going to use it in our application, I just want to check to see that we have everything typed in perfectly. This is a really good idea. Occasionally, you're going to make some typos. You don't want to go too far before you test to see that everything is working properly. So I am going to save and switch over to my app and refresh, and it says you have three pages.

Sometimes you will see this kind of weird scrolling. This is because the canvas iframe is sort of getting cropped off. You can refresh the page. That sometimes helps a little bit or just kind of scroll up and down, which is what I am doing here. So it's correctly saying that since I am dividing my pages three users at a time, this application is going to end up having three pages. In order to get to each of these pages, we are going to need to apply an offset to our Open Graph path. To apply an offset to our Open Graph path, we are going to be creating links on our page with a variable called offset.

We need to go ahead and create a variable that reads the offset from the URLs we will be creating. So I am going to go all the way to the top of my file, and right after I get this quantity variable I want to create a $currentoffset variable and set it to read the server command $_GET and get a value from URL called offset. So now we'll modify our paths take advantage of this new value.

Now that I have this, I am going to create a couple of other variables. So I'll scroll down all the way down here, and I'll create a variable for the number of the current page. That's going to be the value of the currentoffset divided by the amount of users I want to display per page. Normally, that would return a set of values that start with 0, so I am going to add 1 to it. And then finally, I am going to get the value of the $nextoffset, what I need to send to the next page to get the next number of users.

I'll add some comments here. As I said, it's always a good idea to make sure that you check everything. I am just going to copy this line, paste it a couple times, and just modify the variables that I am getting here just to see if I have done everything correctly. So this is going to be Current Page, and this is going to be Next Offset, always a good idea to check your work. So we'll save that. Let's go ahead and go to our app and refresh it. Hopefully, we won't get any errors. Excellent.

And if we scroll it to the bottom, we should see all that information. So this app is going to have three pages. This is page number one, and the next offset will start after user number three. So just to make sure, let me go back and delete these three lines. I am not going to need them in my final application, I will refresh. Make sure that data is no longer there. I just want to have how many friends the user has and then I want to go back to my page. So to create pagination you need to figure out the amount of users to list per page, how you want to offset each call to the Open Graph API, and based on that you can set up variables that calculate the total amount of pages, the current page number, and the offset of the next page.

Since we have that ready, we will be tackling the code for showing our pagination in the next movie.

Show transcript

This video is part of

Image for Building Facebook Applications with PHP and MySQL
Building Facebook Applications with PHP and MySQL

24 video lessons · 8911 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Building Facebook Applications with PHP and MySQL.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.