Javascript: Function that returns status for specified Facebook API permission

I’m working on an application where I want to use Facebook user authorization.

Being someone who is wary of over-granting permission to applications there are permissions I only want to ask for if the user is going to do something where they’re needed.

One of these is the friends list.  As the application will allow the user to invite other people as administrators and managers for their particular group,  I only want access to the friends list if this is something they want to do.

I came up with the following function to do the check for a specific permission.

Its important to remember that the user must grant you permission to access their profile,  and are logged in prior to using this function.

First you must ensure that you do initialize your Facebook connection using your application ID.


window.fbAsyncInit = function() {
FB.init({
appId : 'your app id here',
xfbml : true,
version : 'v2.2'
});

You can get your Facebook App ID here.

In testing, I called it after getting a connected status response from FB.getLoginStatus.

This is the code that calls the function.

This function is synchronous!  That means the script will NOT wait for it to return a value.

….so call it and store the values on initialization, or page load….or before you need it.


FB.getLoginStatus(function(response) {
if (response.status === 'connected') {

FB.api('/me', function(medata) {

//do stuff here

});
checkfbpermission('email');
});
}
});

If you’re wondering what the medata object looks like…here it is:

{"id":"myfbidnumber",
"email":"david.rothbaueratmyemail",
"first_name":"David",
"gender":"male",
"last_name":"Rothbauer",
"link":"https://www.facebook.com/app_scoped_user_id/myidnumber/",
"locale":"en_US",
"name":"David Rothbauer",
"timezone":-4,"updated_time":"2015-04-27T21:01:49+0000",
"verified":true}

Now, the function itself:

function checkfbpermission(perm) {
 FB.api('/me/permissions',function(data){

 /*
I loop through the child objects permission and status sequentially, the found boolean will tell me when to return the status value
*/

 var found = false;  
 
 for (var k in data) {
 
 console.log(JSON.stringify(data[k])); //this is useful for checking that the permission that you're looking for actually exists!
 
 for (var i=0;i<data[k].length;i++)
 
 for (var x in data[k][i]) {
 
 if (found == true) {
  return data[k][i][x];
  } 
 
 if (x == "permission" &&
 data[k][i][x] == perm) {

 //this will tell the script to return the value of the next object (status)
 found = true;
 
 }
 else {
 
 found = false;
 }
 }
 }
 return 'no data';
 
 });

I’m still working out how to best leverage Facebook user integration into my application. I will post more solutions that I think might be helpful as I develop them.

JQuery Quick Tip: Custom Attributes

For quite some time,  whenever I needed to pass something like a database table row index from a webpage to a PHP script I would do string manipulation to get the data.

Something like this:

<div class="tdiv" id="td_4932">Something interesting</div>

<script>

var tid=this.id;

tid = tid.replace('td_','');

//now tid = the index

</script>

 

In an epic /facepalm moment I realized that I’ve been doing it the hard way.   I realized this when I had to pass several bits of information back to the PHP script.  The string manipulation code would be kludgetastic.

It was simple. Put the parameter as a custom attribute!

Like this:

<div class="tdiv" id="tdept" dept="4932">Something interesting</div>  //the dept attribute points to the target department in the database

<script>

var tid = $('#tdept').attr('dept');

console.log("The dept index is " + tid);

</script>

 

PHP: Create HTML Table Row from DB Quick and Easy

One of the things that I find myself doing is building tables to display data from a database query.

My preferred method of doing this is using a JQUERY Post call to a PHP script.

I won’t bore you with the PDO connection strings and database query and cut right to the chase:

<?php
/* do all your db connection stuff and query here */
/* The presumption here is that your $query will return the fields ID,Name and Some Interesting Data, IN THAT ORDER */

$output = '<table>';
$output .= '<tr><th>ID</th><th>Name</th><th>Some Interesting Data</th></tr>';
foreach($query as $row) {   

    $output .= '<tr><td>'.implode('</td><td>',$row).'</td></tr>';      //pretty slick eh?  No concatenating a bunch of cells

}

$output .= '</table>';

echo $output;
unset($dbh);
exit;
?>

Welcome!

Hi There!

Welcome to another of the hundreds, upon thousands of programming blogs in the universe!

My intention here is to provide code snippets in various languages and platforms that I find useful.   I have a talent for explaining things in ways that I think are easy to understand.

If you’re brand new to programming and looking to learn from scratch,  this probably isn’t the place for you.   Once you have a basic understanding, you’re welcome to peruse snippets and pick up some tips.

If you’re looking through my stuff and see something that’s wrong, or that there’s a better way,  post it!

I’m a self-taught programmer.  I don’t know everything (don’t tell anyone I said that!),  and there are probably better ways to do some of the things that I demonstrate here.

My posts will hopefully give you the basics of what I’m trying to show you.

One of the things that I find very annoying about some tutorials and sample code,  is that the author gives you a step by step on setting up a working example…often without sufficient explanation for you to take the bits you need to bend them to your purpose.

I will try to avoid that.

Now…I probably won’t post frequently…but hopefully what I post will be helpful to someone.