0

以及下一个功能中提到的显示好友功能

 function displayfriends($major, $friends) {
    // Whatever markup you want here
    // For example -- unordered list
if (count($friends) > 0) {
echo "<h2>Friends with $major major</h2>";
echo '<ul>';
foreach ($friends as $friend) {
    echo "<li>$friend</li>";
}
echo '</ul>';

} }

这是一个与 UI 配合使用的测试功能
 function getFriendsWithMajor($major) {
 $config = array(
 'appId' => '',
 'secret' => '',
 );
 $facebook = new Facebook($config);
 $user_id = $facebook->getUser();
 try {
 $fql    =   "select uid,name,education from user WHERE uid IN (select uid2 from friend where uid1=($user_id))";
 $param  =   array(
 'method'    => 'fql.query',
 'query'     => $fql,
 'callback'  => ''
 );
 $fqlResult   =   $facebook->api($param);
 } catch(Exception $o) {
    d($o);
 }

 $friends = $fqlResult;
 $friends_BA = array();

 foreach ($friends as $friend) {
     if (is_array($friend['education'])) {
         foreach ($friend['education'] as $school) {
             if (isset($school['concentration'])) {
                foreach ($school['concentration'] as $concentration) {
                    if (strpos(strtolower($concentration['name']), $major) !== false) {
                        $friends_BA[] = $friend['name'];
                        continue 3; // skip to the next friend
                    }
                }
             }
        }
    }
}
$this->displayfriends($major);

}

这是我得到的输出:警告:缺少 social::displayfriends() 的参数 2,在第 234 行的 /home/content/07/8316707/html/class.Social.php 中调用并在 /home/content/ 中定义07/8316707/html/class.Social.php 第 183 行

请记住,当我执行 $this->displayfriends($major, $friends) 时,它只返回了一个结果,而它本应返回 15。

4

1 回答 1

0

我想我明白你在找什么。您基本上只需要了解 php 数组,以及如何将参数与函数一起使用。print_r 是一个调试工具,可以生成您看到的输出。为了使用您的数组数据,您应该直接处理它。最简单的方法是通过它进行遍历。我为显示器制作了一个单独的功能,只是为了强化这两个概念。

function getFriendsWithMajor($major) {
     $config = array(
     'appId' => 'XXXXXXX',
     'secret' => 'XXXXXXXXXX',
     );
     $facebook = new Facebook($config);
     $user_id = $facebook->getUser();
     try {
     $fql    =   "select uid,name,education from user WHERE uid IN (select uid2 from friend where uid1=($user_id))";
     $param  =   array(
     'method'    => 'fql.query',
     'query'     => $fql,
     'callback'  => ''
     );
     $fqlResult   =   $facebook->api($param);
     } catch(Exception $o) {
        d($o);
     }

     $friends = $fqlResult;
     $friends_BA = array();

     foreach ($friends as $friend) {
         if (is_array($friend['education'])) {
             foreach ($friend['education'] as $school) {
                 if (isset($school['concentration'])) {
                    foreach ($school['concentration'] as $concentration) {
                        if (strpos(strtolower($concentration['name']), $major) !== false) {
                            $friends_BA[] = $friend['name'];
                            continue 3; // skip to the next friend
                        }
                    }
                 }
            }
        }
    }
    displayfriends($major, $friends_BA);
}

显示功能

function displayfriends($major, $friends) {
        // Whatever markup you want here
        // For example -- unordered list
  if (count($friends) > 0) {
    echo "<h2>Friends with $major major</h2>";
    echo '<ul>';
    foreach ($friends as $friend) {
        echo "<li>$friend</li>";
    }
    echo '</ul>';
  }
}

调用函数:

getFriendsWithMajor('**business**);
getFriendsWithMajor('**marketing**);
于 2012-05-10T02:32:03.480 回答