Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/ARRAY/HTML TABLE——为什么这会在整个系统中重复相同的事情<;td>;?_Html_Arrays_Facebook_Facebook Graph Api - Fatal编程技术网

PHP/ARRAY/HTML TABLE——为什么这会在整个系统中重复相同的事情<;td>;?

PHP/ARRAY/HTML TABLE——为什么这会在整个系统中重复相同的事情<;td>;?,html,arrays,facebook,facebook-graph-api,Html,Arrays,Facebook,Facebook Graph Api,我正在尝试将一组好友姓名:Facebook图形API中的好友['name']转换成一个表,表中有4列,每个框中有一个人名 问题是: 我让它制作一个表格,并将人们的名字放在表格的单元格中,然而,它在一行中的所有4个人中重复每个人的名字,而不是在每个框中放一个唯一的名字。但是,下一行以新名称开始 如果有人能指出我的错误并帮我纠正,我将非常感激: <? $friends_url = "https://graph.facebook.com/".$user_id."/friends

我正在尝试将一组好友姓名:Facebook图形API中的好友['name']转换成一个表,表中有4列,每个框中有一个人名

问题是:

我让它制作一个表格,并将人们的名字放在表格的单元格中,然而,它在一行中的所有4个人中重复每个人的名字,而不是在每个框中放一个唯一的名字。但是,下一行以新名称开始

如果有人能指出我的错误并帮我纠正,我将非常感激:

    <? 
    $friends_url = "https://graph.facebook.com/".$user_id."/friends?access_token=".$access_token;
    $friends_json = file_get_contents($friends_url);
    $friends_data = json_decode($friends_json, true);       
    $friends_total = count($friends_data['data']);
    $cols = 4;
    for ($i = 0; $i < $friends_total; $i++) 
    {
            $friends = $friends_data['data'][$i];
            echo "<tr>"; 
                for ($c=0; $c<$cols; $c++) 
                {
                $n = $i+$c;
                    if ( $n < $friends_total) 
                    {
                        echo "<td>".$friends['name']."</td>"; 
                    }
                    else
                    {
                        echo "<td></td>";
                    }
                } 
            echo "</tr>"; 
        $i += ($c-1); 
    } 
    ?>

因为您为每个朋友循环了4次,并且“columns”的循环计数器在该循环中,所以请尝试以下操作:

$cols = 4;
$current_col = 0;

    echo "<tr>"; 
    foreach ($friends_data['data'] as $friend)
    {
      echo "<td>".$friend['name']."</td>"; 
      if (($current_col++)%$cols == 0){
        echo "</tr><tr>";
      }
    }
   while (($current_col++)%$col !=0){
   echo "<td>&nbsp;</td>";
   }
   echo "</tr>"; 
$cols=4;
$current\u col=0;
回声“;
foreach($friends_data['data']作为$friends)
{
回显“$friend['name']”;
如果($current_col++)%$cols==0){
回声“;
}
}
而(($current_col++)%$col!=0){
回声“;
}
回声“;
这个循环遍历所有的朋友,保存一个计数器,记录有多少人被打印出来。在4(/或$cols设置为任何值)之后,它会吐出一个新行
当所有的朋友都打印出来后,它开始一个新的循环,用空单元格填充最后一行的末尾,然后结束这一行

啊,谢谢你,我知道是这样的,我只是不知道哪里错了。