Mohammad Shafiq
Mohammad Shafiq

Reputation: 23

How to display pictures of my Facebook friends in a calendar

I'm developing a Facebook app in which I want to display the picture of my friend against his birth date. For example, today is April 26; if any of my friend's birthdays are today, his/her picture should display in the calendar slot for the 26th, etc.

Here's my query so far:

$dt = "April 12";

$fql1 = "SELECT uid,pic_big,pic,pic_small,name 
    FROM user 
    WHERE birthday='".$dt."' AND 
        uid IN (SELECT uid2 FROM friend WHERE uid1= $user)";

I want to be able to handle both full dates (i.e. April 12, 1986) and partial dates (April 12).

This is my Calender code

<table width="100%" cellpadding="0" cellspacing="0" height="400"  align="center" style="border:1px solid #cccccc;">
    <tr><td colspan="3" align="center" class="viewlink"><?php echo $msg;?></td></tr>
    <tr >

    <td width="120" colspan="1" >
        <input type="button" value=" < " onClick="goLastMonth(<?php echo $month . ", " . $year; ?>);">
    </td>
    <td colspan="5" width="610" align="center">
        <span class="title"><?php echo $monthName . " " . $year; ?></span><br>
    </td>
    <td width="120" colspan="1" align="right" style="padding-right:2px;">
        <input type="button" value=" > " onClick="goNextMonth(<?php echo $month . ", " . $year; ?>);">
    </td>
</tr>
<tr>
    <th>Sunday</td>
    <th width="45">Mondy</th>
    <th width="89">Tuesday</th>
    <th width="126">Wednesday</th>
    <th width="98">Thursday</th>
    <th width="69">Friday</th>
    <th>Saturday</th>
</tr>
<tr>
<?php
    for($i = 1; $i < $numDays+1; $i++, $counter++){
        $dateToCompare = $month . '-' . $i . '-' . $year;
        $timeStamp = strtotime("$year-$month-$i");
        //echo $timeStamp . '<br/>';
        if($i == 1){
            // Workout when the first day of the month is
            $firstDay = date("w", $timeStamp);
            for($j = 0; $j < $firstDay; $j++, $counter++){
                echo "<td>&nbsp;</td>";
            }
        }
        if($counter % 7 == 0){
            ?>
            </tr><tr>
            <?php
        }

        $pdate=strlen($i);
        if($pdate==1)
        {
            $day="0".$i;
        }
        else
        {
            $day=$i;
        }

        $pmonth=strlen($month);
        if($pmonth==1)
        {
            $mon="0".$month;
        }
        else
        {
            $mon=$month;
        }

    ?>

    <!--right here--><td width="323" <?=hiLightEvt($month,$i,$year);?> valign="top" style="font-family:Verdana, Geneva, sans-serif; font-size:12px; color:#960; text-align:center; padding:10px; margin:0; border:1px dotted #cccccc;" class="viewlink" ><?=$i;?><br />

    <?php
        **/////////////////////////////Here I want to display friends picture whose birthday at relevant date//////////////////////**
}

?>

Upvotes: 0

Views: 1292

Answers (1)

Jim Rubenstein
Jim Rubenstein

Reputation: 6920

Your question is a little hard to understand; but from what I do understand - you're having a problem finding your friends' birthdays for a specific date.

I would suggest going about this problem in a different way. It looks like you're doing a single FQL query for each date in your calendar. I think it'd be better to load all of a users' friends, and loop through each of them and extrapolate the date. According to Facebook's documentation, the birthday date format is relative to the users' locale, so you are not going to be able to reliably query against that field.

An alternative: (edited to be more complete. almost completely copy and pasteable now.)

<?php

$facebook = new Facebook(array(
    'appId' => FB_APP_ID, //put your FB APP ID here
    'secret' => FB_APP_SECRET, //put your FB APP SECRET KEY here
    'cookie' => true
));

$session = $facebook->getSession();

if ($session)
{
    //check to see if we have friends_birthday permission
    $perms = $facebook->api('/me/permissions');
}

 //we do this to see if the user is logged & installed
if (empty($session) || empty($perms['friends_birthday']))
{
    //get url to oauth endpoint for install/login
    $loginUrl = $facebook->getLoginUrl(array(
        //put the URL to this page, relative to the FB canvas
        //(apps.facebook.com) here vvv
        'next' => 'http://apps.facebook.com/path_to_your_app/index.php',
        'req_perms' => 'friends_birthday'
    ));

    //use javascript to redirect. the oauth endpoint cant be loaded in an
    //iframe, so we have to bust out of the iframe and load it in the browser
    //and tell the oauth endpoint to come back to the fb canvas location
    echo "<script>window.top.location='{$loginUrl}';</script>";
    exit;
}

$user = $session['uid']; //load the user id from the session array

$cur_month_birthdays = array();
$cur_month = 4; //april

try {

    $fql1 = "SELECT uid, pic_big, pic,pic_small, name, birthday FROM user "
    .   "WHERE uid IN ( "
    .       "SELECT uid2 FROM friend WHERE uid1 = {$user} "
    .   ")";

    $friends = $facebook->api(array(
        'method'    => 'fql.query',
        'query'     => $fql1
    ));

    //make sure i have some friends...
    if (!empty($friends)) 
    {
        foreach ($friends as $friend)
        {
            //if this friend doesn't have their bday specified, skip them.
            if (empty($friend['birthday']))
            { 
                continue;
            }

            //get unix time for the users' birthday
            $birthday_ts = strtotime($friend['birthday']);

            //if this friends birthday is this month...
            if (date('m', $birthday_ts) == $cur_month)
            {
                //generate a month-day string for the birthdate
                $birthday_str = date('m-d', $birthday_ts);

                //initialize the array of friends with birthdays on this date
                if (empty($cur_month_birthdays[ $birthday_str ]))
                {
                    $cur_month_birthdays[ $birthday_str ] = array();
                }

                $cur_month_birthdays[ $birthday_str ] []= $friend;
            }
        }
    }
}
catch (Exception $e)
{
    //error with facebook
    error_log($e);
}

//output list of days in this month with friends that have birthdays
print_r($cur_month_birthdays); 

The above code will fetch all your users' friends, loop through each, and find the ones with a birthday that is in your current month (defined as $cur_month). You can then loop through and build your calendar, and for each day you check the $cur_month_birthdays array to see if there are any friends that have a birthday on the current day, and display them appropriately.

Hope that helps!

** edit **

This is my output for the above script. I have edited it to be more thorough it's set-up (including initialization of the FB PHP SDK and fetching of the users' session. I had assumed that you had at least that code in place. Now that you can see the structure of the array - you should be able to easily figure out how to integrate into your calendar.

Array
(
    [04-04] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Thomas W
                [birthday] => April 4, 1985
            )

    )

    [04-19] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Joel S
                [birthday] => April 19
            )

    )

    [04-29] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Ashley F
                [birthday] => April 29, 1983
            )

            [1] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Megan S
                [birthday] => April 29, 1989
            )

    )
)

Upvotes: 1

Related Questions