Home
Username:
Password:
Facebook Application Development Tutorials

Adding Data to a Facebook App / Finishing the Add to My Picks




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 that we understand the structure of those three tables let's go back and look at what happens when we add a pick. We've already talked about the add pick file and what we've talked about is how we inserted a new record into the picks table and we published news about it. Then we redirect to another file called add remove myPick. This is the file that handles, as its name implies, adding or removing a pick that already exists from the list of a user's picks and what that means in practice is adding or removing a record from the user picks table. That's what it does. So we redirect to add remove myPick and we pass in an action which is going to be add. We pass in the facebook user ID and the pickid and let's see what happens when we come into this file. What we're doing here is we're doing our standard includes up here and we're connecting to Facebook and then we are unpacking, as we usually do in these files, we are unpacking the parameters that are coming in and placing them in local variables. Here we have the facebook user ID, the pickid and the action. Now we come down here and this section of code is for the case of the action being add and the first thing that we do is the same sort of thing that we did when we were adding the pick. We do a count, a count is a very fast operation because it normally runs off an index. We count the number of records in user picks where the facebook user ID is the one that was passed in and the pickid is the pickid that was passed in. And if we find none, that's down here, this is the boilerplate code to execute the query to get the result and to store it in a variable called row, which is an Array and we look at row of zero and if theCount is zero that means there is no userpicks record, so we want to add one. If theCount is not zero then we don't do anything. Because there's something there, there's no reason to add it. It's not an error, they're just trying to add it a second time. In the interface we should be preventing them from having the option to add it to their pick list but it doesn't matter. It's there if they want to add it it's there already, just don't add it a second time. That's what theCount does. And then we do a standard insert to INSERT that record INTO userpicks and execute the query and then we go back to the page of my picks to display the list of my picks. So this file can be called either automatically as we got into it in this discussion; it can be called after you add a pick and we automatically add it to your list of picks, or it can be called, if you've clicked on Add to my Picks for an already existing pick, but then we go back and we show you your list of picks. If it's not add, if the action is removed then we do a delete. We don't have to check to see whether it's there or not. If it's not there we'll get an error but it's not going to be reported to the user. We just delete it. If it's deleted terrific, if it wasn't there who cares? Doesn't matter. And that's the way in which we maintain the userpicks table. And at the end unless we are adding a record, in which case we go to the list of myPicks we will go to a default place which also happens to be myPicks. See your list of picks after you've added one, or after you've deleted one. It's the most logical place to go and that's the simple way in which we maintain the join table, userpicks and maintain therefore the list of picks for every single user who's using the Facebook App.

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