简体   繁体   中英

Proper way to choose specific data after json_decode to display on webpage from Facebook Albums

I believe I am possibly going about this either the wrong way or hard way. I've spent the last +24 hours reading posts and experimenting to try to get the result I want. I grabbed different code snippets from different questions here until I finally got something that somewhat that worked. I even read about arrays vs objects but still confused on what route I should have went.

I am currently using the JS-SDK, I think. (I have the Simple-Facebook-Connect wordpress plugin installed) but I am not sure if that is why I am able to do what I am doing.

To get to the point, I want to display Albums from a Facebook "Page" onto my website. I am able to grab all the proper data with:

<?php

$fbc_jsonurl  = "https://graph.facebook.com/(My_Page_ID)/albums";
$fbc_user = json_decode(file_get_contents($fbc_jsonurl));
//$fbc_user = json_decode(file_get_contents($fbc_jsonurl),true); Was experimenting with the "true" statment since some people were using it but then none of my code worked right
print_r($fbc_user);

?>

The above code was able to grab all the data and print it to screen.

So I then proceeding and used the following code, this code basically grabbed the name of ALL albums and only printed the album names to the screen with no other information:

foreach($fbc_user->data as $fbc_testing) {
    $fbc_albums[] = $fbc_testing->name;
}
print_r($fbc_albums);

So I experimented and tried to take it one step further and used the following code:

foreach($fbc_user->data as $fbc_testing) {
    $fbc_albums[0] = $fbc_testing->name;
    $fbc_albums[1] = $fbc_testing->link;

}
print_r($fbc_albums);

This code displayed an Album Title and the proper link to that album. The problem is, this time it only displayed 1 listing instead of all of them.

What I would like to do at first is create an HTML Ordered List that displays Album Title, Album Link, Album Description, Album Cover Image, and how many images are in the album. Doesn't need to be pretty because I can use CSS to fix it up.

Would also be awesome but might be to much for this one question here, but if:

  1. I can choose how many albums to display.
  2. Don't display albums unless it has a minimum amount of images.
  3. Pagination
  4. Clicking on an album takes you to another page on my site which displays the images from that album.

I have read about the FQL but to be honest getting what I got above was very difficult and I am still confused about why some of it works.

In your example, the problem is that you're only ever writing the first and second (0,1) elements of your display array in your foreach loop.

You have a number of different options here. You could create some kind of FacebookAlbum class to store all of the data, you could simply put the data into an array like you currently are or you could just leave the data in the object which facebook has returned .

If you just wanted to get the name and link for each album, then modify your code like so:

foreach($fbc_user->data as $fbc_testing) {
    $fbc_albums[] = Array("name" => $fbc_testing->name, "link" => $fbc_testing->link);
}
print_r($fbc_albums);

Obviously you're looking for a more complex solution. Here's an example of something you could do to cover the first few points.

<?php

function displayAlbums($albums, $numberToShow, $minPhotos) {
    $count = 0;
    foreach ($albums->data as $a) {
        if ($count > $numberToShow)
            return;
        if ($a->count > $minPhotos) {
            //Print out your html.
            //Album name is $a->name, link is $a->link etc

            $count++;
        }
    }
}

$fbc_jsonurl  = "https://graph.facebook.com/(My_Page_ID)/albums";
$fbc_user = json_decode(file_get_contents($fbc_jsonurl));
displayAlbums($fbc_user,10,20); // Show the first 10 albums with at least 20 photos.
?>

Obviously in a solution like that you're only going output the 'first' 10 albums as Facebook has ordered them, so unless you can specify an order in your Facebook Graph query, you might not always get the results that you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM