Dynamic Colspan - Php - Stack Overflow

Here's something to get your started. It is probably not the most elegant, but it will do.

<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>

If there are 3 table cells, it isn't really possible with the method above, because colspan can only span whole table columns (no fractions).

So, instead, put a table inside a table cell spanning the whole row...

<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>

Từ khóa » Html Colspan Dynamic