-2

I have this code right here, where the $friends variable is an array with an output like this:

{
  "id": "1149862205",
  "name": "name",
  "first_name": "first_name",
  "last_name": "last_name",
  "link": "https://www.facebook.com/username",
  "username": "username",
  "birthday": "07/12/1990",
  "hometown": {
    "id": "108618265828550",
    "name": "name"
  }, 

while $fbfriendlikes variable is generated by the id of $friends and has this structure :

{
  "data": [
    {
      "name": "Penelope-ns-test",
      "category": "Community",
      "id": "187099821418102",
      "created_time": "2012-06-14T15:00:59+0000"
    },

Now, I need to print all $friends ids which like a specific id in $fbfriendlikes My code goes like this:

$friends = $facebook->api('/me/friends');
if (!empty($friends['data'])) {
    $size = variable_get('facebook_graph_pic_size_nodes','square');
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    foreach ($friends['data'] as $data) {
        $fbid = $data['id'];
        $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); 
        $fbname = $data['name'];
        $path = $protocol . '://graph.facebook.com/' . $fbid . '/picture?type=' . $size;
        $image = theme('image', array('path' => $path, 'alt' => $fbname));

        if ($fbfriendlikes["id"] != 184759054887922) {
            $return .= '<div class="fimage">'.$image.'</div>';
            $link = '<a href="'.$protocol . '://www.facebook.com/profile.php?id='.$fbid.'" target="_blank">'.$fbname.'</a>';    
            $return .= '<div class="flink">'.$link.'</div>';
            break;
        }
    }
}

I know I have to do a foreach loop in $fbfriendslikes but I can't seem to make it work. Can you please give me an advice?

3
  • try foreach($friends["data"] as $FData => $Value), $FData is the array key , and $Value is the value.. Commented Jun 26, 2012 at 8:21
  • Hey thanks for the answer, so understand this if i'm correct please. What i need is to extract the id from $friends, this id i use it to extract data from $fbfriendlikes. What i need is to print the information from $friends but the condition has to be based on $fbfriendslikes. And please can you be more specific in where to edit the code?.. thansk Commented Jun 26, 2012 at 8:34
  • possible duplicate of Loop through array to get value Commented Jun 26, 2012 at 11:36

2 Answers 2

0

You need to check inside you loop to get the ones that don't like.

$return = '';
$friends = $facebook->api('/me/friends');
if (!empty($friends['data'])) {
    $size = variable_get('facebook_graph_pic_size_nodes','square');
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    foreach ($friends['data'] as $data) {
        $fbid = $data['id'];
        $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); 
        $fbname = $data['name'];
        $path = $protocol . '://graph.facebook.com/' . $fbid . '/picture?type=' . $size;
        $image = theme('image', array('path' => $path, 'alt' => $fbname));

        foreach($fbfriendlikes['data'] as $like) {
            $likes = false; // set var to check for like
            if($like['id'] == '184759054887922') { // check if they liked it
                $likes = true; // set var to true because they liked it
            }
            if (!$likes) { // test var to see if false (they did not like)
                // print this if they did NOT like it, otherwise do nothing
                $return .= '<div class="fimage">'.$image.'</div>';
                $link = '<a href="'.$protocol . '://www.facebook.com/profile.php?id='.$fbid.'" target="_blank">'.$fbname.'</a>';    
                $return .= '<div class="flink">'.$link.'</div>';
            }
        }// go on to next in loop

    }
    echo $return;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks, so the way you posted it is supposed to be the right way? Thanks!
given the way you presented your data, and the questions, I believe so. Essentially if you want to find those that DON'T like something, create a variable that is defaulted to false, you want to loop through them all, and set that to true (I edited my code so look back at it) if they like it, set it TRUE, and check that var. If it is false )they didn't like it), print whatever, if they did, do nothing and continue loop.
0

Try this:

$return = '';
$friends = $facebook->api('/me/friends');
if (!empty($friends['data'])) {
    $size = variable_get('facebook_graph_pic_size_nodes','square');
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    foreach ($friends['data'] as $data) {
        $fbid = $data['id'];
        $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); 
        $fbname = $data['name'];
        $path = $protocol . '://graph.facebook.com/' . $fbid . '/picture?type=' . $size;
        $image = theme('image', array('path' => $path, 'alt' => $fbname));

        $likes = false;
        foreach($fbfriendlikes['data'] as $like) {
            if($like['id'] == '184759054887922') { // maybe $like->id will have to be used instead...
                $likes = true;
                break;
            }
        }

        if (!$likes) {
            $return .= '<div class="fimage">'.$image.'</div>';
            $link = '<a href="'.$protocol . '://www.facebook.com/profile.php?id='.$fbid.'" target="_blank">'.$fbname.'</a>';    
            $return .= '<div class="flink">'.$link.'</div>';
        }
    }
    echo $return;
}

4 Comments

Did a few modifications. Try it now.
You see, thanks for the answers but the problem is that it seems like the if condition won't affect the result!
What i'm trying to reach to do is: if you go here and do fconnet, the first button on right, penelope-ns.net/fb it's suppose to get first friends id, then based on the id's retrieved by $fbfriendlikes i get for each friend the pages they like, then i echo all the friends of the user who don't like a specific page id, which is generated by $fbfriendlikes[id] ...
Besides, isn't it supposed to be something like [data][0][id]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.