doudi2005 2013-05-13 21:05
浏览 10
已采纳

限制朋友的数量到12个框中显示?

i have a box which displays friends of a user, however, it shows them all and stretched off the canvas of the app. I have them in a table in the box, is there anyway to limit the amount shown to 12 random friends?

this is my code to display friends:

if ($user) {
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends');

echo '<table>';
foreach ($friends["data"] as $value) {
    echo '<td>';
    echo '<div class="pic">';
    echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
    echo '</div>';
    echo '<font color="white">','<div class="picName">'.$value["name"].'</div>','</font>';
    echo '</td>';
}
echo '</table>';
}
?>
  • 写回答

2条回答 默认 最新

  • dsnw2651 2013-05-13 21:22
    关注

    You could, for instance, use the break statement to exit the loop. Use it like this:

    if ($user) {
        $user_profile = $facebook->api('/me');
    $friends = $facebook->api('/me/friends');
    
    $counter = 1;
    
    echo '<table>';
    foreach ($friends["data"] as $value) {
        echo '<td>';
        echo '<div class="pic">';
        echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
        echo '</div>';
        echo '<font color="white">','<div class="picName">'.$value["name"].'</div>','</font>';
        echo '</td>';
    
        if ($counter >= 12) {
            break;
        }
        $counter = $counter + 1;
    }
    echo '</table>';
    }
    

    A more advanced approach would make use of the array_slice() function, which modifies arrays. To only get at most 12 elements, starting at position 0, of $friends['data'] into the foreach loop, you would write:

    // your code...
    foreach (array_slice($friends["data"], 0, 12) as $value) {
        // your code...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在ns3中实现路径的自由切换
  • ¥20 SpringBoot+Vue3
  • ¥15 IT从业者的调查问卷
  • ¥65 LineageOs-21.0系统编译问题
  • ¥30 关于#c++#的问题,请各位专家解答!
  • ¥15 App的会员连续扣费
  • ¥15 不同数据类型的特征融合应该怎么做
  • ¥15 用proteus软件设计一个基于8086微处理器的简易温度计
  • ¥15 用联想小新14Pro
  • ¥15 multisim中关于74ls192n和DSWPK开关仿真图分析(减法计数器)