how to get user's random friend in facebook application? - php

I have a friendlist that displays all of user's facebook friends.
What I actually want is to display only one friend at a time, selected randomly.
is there any way to do so?
<?
foreach ($friends as $friend)
{
echo "<img src='http://graph.facebook.com/" . $friend['id'] ."/picture'><br>";
}
?>

<?
$friend_count = count($friends);
$random_friend_num = rand(0,$friend_count-1);
$random_friend = $friends[$random_friend_num];
echo "<img src='http://graph.facebook.com/" . $random_friend['id'] ."/picture'><br>";
?>
The code is pretty much self explanatory..
Get Friend count
get Random number in range 0 to friendCount
paste Random friend
If my answer is what you needed, feel free to accept it :)

Related

Print an array value when its key matches with a variable (PHP)

I have a "album.php" page with four albums; when you click on one of them it links to "gallery.php" showing the photos of that specific album.
Now I would like to put a specific title in "gallery.php" depending on the album clicked by the user and I would like to do it in a nice and clean way.
gallery.php:
$query_url = $_SERVER['QUERY_STRING'];
$pageID = substr($query_url, 6);
$album_title = array("album1" => "title1",
"album2" => "title2",
"album3" => "title3");
while($album_title[KEY] = $pageID)
{
echo '<div id="album_title">
<h2 class="body_text">'.$album_title[VALUE].'</h2>
</div>';
}
I thought something like that instead of the less dynamic:
if($pageID = "album1")
{echo "title1";}
if($pageID = "album2")
{echo "title2";}
etc.
My problem is that I don't know how I could "say":
when(array[KEY] = $pageID)
{
echo 'the correspondent array[VALUE]';
}
Thanks everyone!
[EDIT] Voting negatively without an explication doesn't solve any problem in this world, mate. Was probably 'cause I used "while" instead of "foreach" in my example? I know about its existence but I tried multiple times with it also without success.
Provinding $pageID is something like "album1" you wouldn't need any loop:
echo '<div id="album_title">
<h2 class="body_text">' . $album_title[$pageID] . '</h2>
</div>';
Check it out.

Issue echoing links PHP MySQL, missing code

I am having trouble with my code. I want to display posts on one page grouped by category but I also need the titles of the posts to be links to the actual articles, everything I’ve tried so far has broken. Can anyone help? I can echo the titles just not as links!
You should take the excellent advice from #Marten and #CharlesAddis in the comments, and if you don't want to use an ORM, you should at least use PDO.
However, to give an answer for answer's sake:
while ($row = mysqli_fetch_array($result)){
echo "<h2>".$row['category']."</h2>"."<br />";
$titles = explode(", ",$row['titles']);
foreach ($title as $t) {
echo '' . $t . '<br>';
}
echo "<hr />";
}
I'm not sure what the relation between post titles and post links are, if you can add that I can update the answer.

Display single latest instagram photo on your website

I am looking all over the web and can't believe I can't find an answer to my question.
I would like to display the latest single instagram photo on my website. simple as that.
No gallery, no fancybox, no slideshow etc etc
I found this useful link:
http://www.blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
it's actually working but it doesn't give an option of how many images you would like to display
and I would like only one latest instagram image.
anyone have an idea?
Thanks
You can break foreach after showing first image like below:
foreach ($result->data as $post) {
// Do something with this data.
break; // for one result only
}
Or add &count=1 at end of url as below
https://api.instagram.com/v1/users/ID-GOES-HERE/media/recent/?access_token=TOKEN-GOES-HERE&count=1
The answer is right here: http://instagram.com/developer/endpoints/users/#get_users_media_recent_with_client_id
Count option for number of media !
Try this:
$page_id = 'YOUR ID';
$access_token = 'YOUR ACCESS TOKEN';
$json_object = #file_get_contents('https://api.instagram.com/v1/users/' . $page_id .
'/media/recent/?access_token=' . $access_token . '&count=1');
$indata = json_decode($json_object);
echo $indata->data[0]->images->low_resolution->url // for low res
echo $indata->data[0]->images->thumbnail->url; //for thumbnail
echo $indata->data[0]->images->standard_resolution->url; //for standard res
echo $indata->data[0]->caption->text; //for caption
Embedding the whole post may be a solution:
$json_post = #file_get_contents('https://api.instagram.com/oembed/?url=' . $indata->data[0]->link);
$oembed = json_decode($json_post, true);
echo $oembed['html'];
Good luck!

facebook api get my friends profile pic

Hi there I am creating an app and I want to display my friends profile picture, but what happens in my case is that my profile picture is displayed x the number of the friends that I have. Any idea of what I should change on my code ? Thanks.
$uid = $facebook->$facebook->api('/me/friends/');
//create the url
$profile_pic = "http://graph.facebook.com/".$uid."/picture";
foreach($user_graph['data'] as $photokey => $photovalue){
echo "<img src=\"" . $profile_pic . "\" />";
}
From the code you have provided I see a couple of problems. Why are you calling $facebook->$facebook->api()? Surely that should be $facebook->api('/me/friends/');
Secondly, your api call returns an array, it looks like you are trying to treat $uid as a single value to me.
Also, If your loop works I imagine $user_graph must be defined elsewhere in your code.
I would probably just do something like this:
$friendlist = $facebook->api('/me/friends/');
foreach($friendlist["data"] as $value) {
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
}
It's not going to look very neat but it should yield the result you were originally going for.

I am trying to figure out the proper CSS to get this code to work for a Facebook feed on a website

I am using the code below for a Facebook feed I have on a website. Problem is that it lists the images and the news like it is supposed to but for the life of me, I can't figure out how to put the images on their and have the associated text displayed nicely to the right of it. Can someone please help.
Here is the code:
<?php
//Get the contents of the Facebook page
$FBpage = file_get_contents('https://graph.facebook.com/135860723149188/feed? access_token=456215171075777|OcFg4Sf293Pg3NXeJkg1h3Bg8wE'
);
//Interpret data with JSON
$FBdata = json_decode($FBpage);
//Loop through data for each news item
foreach ($FBdata->data as $news ) {
//Explode News and Page ID's into 2 values
$StatusID = explode("_", $news->id);
echo '<li class="fbframe"; style="list-style:none;" >';
//Check for empty status (for example on shared link only)
if (!empty($news->picture)) {
printf ('<img src="%s" alt="Image from Facebook" />', $news->picture);
}
if (!empty($news->message)) { echo $news->message; }
echo '</li>';
} //end of fbframe
?>
Thanks in advance for any help that you can offer.
wrap the <li> elements in an <ul style="list-style:none;">
remove the semi-colon after "fbframe";
Sounds like Nicole Sullivan's media object is exactly what you're looking for, Facebook and all.

Categories