I have a table will show data from a database. These data count is dynamic. I want to show 4 in each row. However, the problem with the last row. I want if the last row has 1 TD the function will give this td a colspan of 4, so I can show the data in the middle.
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 ||
MY PHP CODE + 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>
Thank’s in advance.
Here’s something to get your started. It is probably not the most elegant, but it will do.
If there are 3 table cells, it isn’t really possible with the method above, because
colspancan only span whole table columns (no fractions).So, instead, put a table inside a table cell spanning the whole row…