Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Learn the basics of building complex, data-driven applications with the Facebook PHP SDK and MySQL. Author Ray Villalobos first introduces the fundamentals, such as checking to see if a user is logged into an application, accessing Facebook user data through the Open Graph API, and making complex API calls with the Graph API Explorer. The course then dives into building an application with API paths and Facebook Query Language (FQL) calls. Along the way, you'll discover how to post to a user's wall, upload photos, integrate with webpages, and more.
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.
There are currently no FAQs about Building Facebook Applications with PHP and MySQL.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.