动态的colspan

时间:2012-09-05 09:13:12

标签: php html

我有一个表将显示数据库中的数据。这些数据计数为dynamic。我想在每一行显示4个。但是,最后一行的问题。我想如果最后一行有1个TD,那么这个函数会给这个td {4}的colspan,所以我可以在中间显示数据。

NUMBER OF DATA IS 5

###############################################
#           TABLE HEADER(colspan=4)           #
###############################################
||  TD1   ||  TD2  ||   TD3   ||    TD4     ||
||  TD5   ||  

I NEED THIS 
###############################################
#           TABLE HEADER(colspan=4)           #
###############################################
||  TD1   ||  TD2  ||   TD3   ||    TD4     ||
||                  TD5                     ||  

and if there are 6 TDS to show it like this.

###############################################
#           TABLE HEADER(colspan=4)           #
###############################################
||  TD1   ||  TD2  ||   TD3   ||    TD4     ||
||       TD5       ||          TD6          ||  

我的PHP代码+ HTML

<table>
    <tr>
        <th colspan="4">TABLE HEADER</th>
    </tr>
    <tr>
        <? $counter = 0;
                foreach($data as $d)
                {
                    if($counter % 4 == 0)
                    {
                        echo '</tr><tr>';
                    }
                    echo $d['name'];
                    $counter++
                }
        ?>
    </tr>
</table>

提前感谢。

2 个答案:

答案 0 :(得分:1)

这是让你开始的东西。它可能不是最优雅的,但它会做到。

<tr>
    <? $counter = 0;
       $lastrow = floor($count($data)/4)*4;
       $numinlastrow = $count($data)%4;
            foreach($data as $d)
            {
                if($counter % 4 == 0)
                {
                    echo '</tr><tr>';
                }
                $colspan = 1;
                if ($counter >= $lastrow) {
                  if ($numinlastrow == 1) $colspan = 4;
                  if ($numinlastrow == 2) $colspan = 2;
                  if ($numinlastrow == 3) ; // ??
                }
                echo "<td colspan='".$colspan."'>";
                //... contents of table cell here
                echo "</td>";
                $counter++
            }
    ?>
</tr>

如果有3个表格单元格,则上述方法无法实现,因为colspan只能跨越整个表格列(无分数)。

所以,相反,将一个表放在跨越整行的表格单元格中......

<tr>
    <? $counter = 0;
       $lastrow = floor($count($data)/4)*4;
       $numinlastrow = $count($data)%4;
            foreach($data as $d)
            {
                if($counter % 4 == 0)
                {
                  if ($counter >= $lastrow) {
                    echo '</tr><tr><td colspan="4"><table><tr>';
                  }
                  else echo '</tr><tr>';
                }

                echo "<td>";
                //... contents of table cell here
                echo "</td>";
                $counter++
            }
            if ($numinlastrow != 0) echo '</tr></table></td>'
    ?>
</tr>

答案 1 :(得分:0)

我会以下列方式(大约)这样做 - 虽然这看起来有点傻,但我现在想不出更好的方法:

$num_of_rows = mysql_num_rows($result);
for ($i = 0; $i < ($num_of_rows / 4); $i++)
{
   for ($j = 0; $j < 4; $j++)
   {
      $row = mysql_fetch_assoc($result);
      echo $row['name'];
   }
   echo '</tr><tr>';
}
if (mysql_num_rows($result) == 3)
{
   mysql_fetch_assoc($result);
   echo '<td colspan="2">'.$row['name'].'</td>';
   mysql_fetch_assoc($result);
   echo '<td>'.$row['name'].'</td>';
   mysql_fetch_assoc($result);
   echo '<td>'.$row['name'].'</td>';
}
else
{
   while($row = mysql_fetch_assoc($result))
   {
      echo '<td colspan="'.(4 / mysql_num_rows($result)).'">'.$row['name'].'</td>';
   }
}