PHP/ARRAY/TABLE - 我的表有太多的 <tr></tr>


PHP/ARRAY/TABLE - My table has too many <tr></tr>

我需要一点帮助,我将数组转换为表的循环效果很好,除了它为我的结果增加了大量额外的内容。

这是创建一个表格以显示来自Facebook Graph API的朋友的循环。

<? 
sort($friends_data['data']);
    echo "<tr>"; 
    foreach ($friends_data['data'] as $friend)
    {
    $names = substr($friend['name'],0,1);
        if ($names == 'M') {
             echo "<td><ul><a href='https://www.facebook.com/".$friend['id']."' target='_blank'>".$friend['name']."</a></ul></td>"; 
            }
      if (($current_col++)%$cols == 0){
        echo "</tr><tr>";
      }
    }
   while (($current_col++)%$cols !=0){
   echo "<td>&nbsp;</td>";
   }
   echo "</tr>"; 
?>

这是它吐出的(删除了朋友(:

<table border="0" cellpadding="4" cellspacing="10" width="100%" ><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr><td>&nbsp;</td></tr></table>

($names != 'M')它会给你空<tr>而没有td时,你没有对待任何<td/>你只有($names == 'M') <td>

试试这个

<? 
sort($friends_data['data']);
    echo "<tr>"; 
    foreach ($friends_data['data'] as $friend)
    {
    $names = substr($friend['name'],0,1);
        if ($names == 'M') {
             echo "<td><ul><a href='https://www.facebook.com/".$friend['id']."' target='_blank'>".$friend['name']."</a></ul></td>"; 
            }
        else
        {
             //this of $name is not 'M'
             //assume empty if $name is not 'M'
             echo "<td>&nbsp;</td>";
         }
      if (($current_col++)%$cols == 0){
        echo "</tr><tr>";
      }
    }
   while (($current_col++)%$cols !=0){
   echo "<td>&nbsp;</td>";
   }
   echo "</tr>"; 
?>

或者假设您是否只想显示$names == 'M'

<? 
sort($friends_data['data']);
    echo "<tr>"; 
    foreach ($friends_data['data'] as $friend)
    {
    $names = substr($friend['name'],0,1);
        if ($names == 'M') {
             echo "<td><ul><a href='https://www.facebook.com/".$friend['id']."' target='_blank'>".$friend['name']."</a></ul></td>"; 
            if (($current_col++)%$cols == 0){
                echo "</tr><tr>";
            }
        }              
    }
   while (($current_col++)%$cols !=0){
   echo "<td>&nbsp;</td>";
   }
   echo "</tr>"; 
?>