2011-04-28 132 views
0

我正在開發一個Facebook應用程序,我想在其中顯示我的朋友的照片以及他的出生日期。例如,今天是4月26日;如果我的任何朋友的生日的今天,他/她的照片應該在26日的日曆顯示時隙等如何在日曆中顯示我的Facebook朋友的照片

這裏是到目前爲止我的查詢:

$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)"; 

我希望能夠同時處理完整日期(即1986年4月12日)和部分日期(4月12日)。

這是我的日曆代碼

<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//////////////////////** 
} 

>

+0

你可以嘗試格式化你的代碼,所以的編輯似乎有點堅果最近,但我實在看不出。你的代碼是幹什麼的,因爲它沒有格式化,字符也不存在 – 2011-05-02 03:10:09

回答

1

你的問題有點難以理解?;但根據我的理解 - 您在尋找朋友的特定日期的生日時遇到問題。

我會建議以不同的方式解決這個問題。看起來您正在爲日曆中的每個日期執行單個FQL查詢。我認爲加載所有用戶的朋友會更好,並循環瀏覽每個用戶的朋友並推斷日期。根據Facebook's documentation,生日日期格式是相對於用戶的語言環境而言的,因此您無法在該字段中可靠地進行查詢。

一種替代方案:(。編輯更加完整的,幾乎完全照搬現在pasteable

<?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); 

上面的代碼將獲取所有用戶的通過每個朋友圈,並找到那些在你當前月份的生日(定義爲$cur_month)。然後,您可以循環並構建您的日曆,並且每天您都會檢查$cur_month_birthdays陣列,以查看是否有朋友在當天有生日,並適當地顯示它們。

希望有幫助!

** 編輯 **

這是我對上面的腳本輸出。我已經編輯了它的設置(包括FB PHP SDK的初始化和用戶會話的獲取),我假設你至少在代碼已經到位現在你可以看到結構數組的 - 你應該能夠很容易找出如何融入您的日曆

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 
      ) 

    ) 
) 
+0

感謝Jim的回覆。我把這段代碼嵌入到了這個顯示器中,只顯示Array(),沒什麼其他的,請問我該怎麼把它嵌入到我的日曆中。謝謝 – 2011-04-29 13:17:56

+0

我道歉,我的代碼中有一個錯誤 - 讓我修復它你真的很快。它在第36行,而不是$ friend ['birthday'],您需要使用$ birthday_ts。現在編輯。 – 2011-04-29 13:25:15

+0

至於向您展示如何將其嵌入到您的日曆中,我不知道您的日曆生成腳本的細節,所以我不能很好地做到這一點。但是,$ cur_month_birthdays數組在每月的日期(M-DD)被編入索引,因此您只需檢查那天有生日的人每天的密鑰。一個不存在的鑰匙意味着當天沒有生日,但是如果有生日,那麼每個在當日有生日的用戶將成爲陣列中的一個元素。 – 2011-04-29 13:27:43

相關問題