分享facebook的朋友

时间:2013-12-31 12:26:38

标签: php mysql facebook sorting

    $app_using_friends = $facebook->api(array(
        'method' => 'fql.query',
        'query' => 'SELECT uid, name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1'

      ));

       echo '<pre>';
      print_r("query results:");
      print_r($app_using_friends);
      echo '</pre>';


    foreach($app_using_friends as $fl){

        echo "<li>".$fl['name']."</li>";        
        $result = mysql_query("SELECT * FROM user WHERE fb_id = " . $fl['uid']);
    while ($row = mysql_fetch_array($result)) {
       $test = $row{'fb_score'};
       echo "score:".$row{'fb_score'}."<br>";
    }
    }


query results:Array
(
  [0] => Array
  (
    [uid] => 100004375809
    [name] => Brat
  )

  [1] => Array
  (
    [uid] => 100005832212
    [name] => Vit
  )

  [2] => Array
  (
    [uid] => 100005832323
    [name] => Ala
  )

)
  

小子
  得分:1

     

维生素
  分数:46

     

丙氨酸
  分数:20

你如何按分数排序?

2 个答案:

答案 0 :(得分:0)

使用Order by:

 $result = mysql_query("SELECT * FROM user WHERE fb_id = " . $fl['uid'] . " order by fb_score");

答案 1 :(得分:0)

解决方案1 ​​

您可以将这些结果添加到数组中,并使用usort()函数对数组进行排序。它应该是这样的:

$list = array();
foreach($app_using_friends as $fl){      
    $result = mysql_query("SELECT * FROM user WHERE fb_id = " . $fl['uid']);
    while ($row = mysql_fetch_array($result)) {
        $list[] = $row;
    }
}

function mycmp($a, $b) {
    if ($a['fb_score'] == $b['fb_score']) return 0;
    return ($a['fb_score'] > $b['fb_score']) ? -1 : 1;
}

// Performing the sort
usort($list, mycmp);

var_dump($list);

解决方案2

您可以加入所有朋友的ID并只进行一次MySQL传递。它应该是这样的:

$friend_id = "";
foreach($app_using_friend as $value) {
    if ($friend_id != "") $friend_id .= ","
    $friend_id .= $value['uid'];
}

// Query
$result = mysql_query("SELECT * FROM user WHERE fb_id IN (" . $friend_id . ") ORDER BY fb_score DESC");