php - How can iterate every 5 results in a table? -
i have foreach need distributed every 5 result per row on table.
this current foreach:
<?php $i = 0; echo "<tr>"; foreach($klasifikasi $result){ $i++; if($i % 5 == 0){ echo "<td width='20%' align='center'>".$result['klasifikasi']."</td>"; echo "<td width='20%' align='center'>".$result['klasifikasi']."png</td>"; } }; echo "</tr>"; ?>
it doesn't stop every 5 result , doesn't create new row.
this i'm looking for:
--------------------------------------------------- a1 | a2 | a3 | a4 | a5 --------------------------------------------------- a1.png | a2.png | a3.png | a4.png | a5.png --------------------------------------------------- /*this row should empty spacing*/ --------------------------------------------------- .../*skipped till last row*/ --------------------------------------------------- a21 | a22 | a23 | a24 --------------------------------------------------- a21.png | a22.png | a23.png | a24.png ---------------------------------------------------
my current result iteration 24 data. may become more or less
note:
if possible, every columns not empty should have widht of 20%
a simple solution problem:
$i = 1; $html = "<table border='1'>"; $row1 = '<tr>'; $row2 = '<tr>'; foreach($klasifikasi $result){ $row1 .= "<td width='20%' align='center'>".$result['klasifikasi']."</td>"; $row2 .= "<td width='20%' align='center'>".$result['klasifikasi']."png</td>"; if($i > 0 && $i % 5 === 0){ $row1 .= '</tr>'; $row2 .= '</tr>'; $html .= $row1.$row2; $html .= '<tr><td colspan="5"></td></tr>; $row1 = '<tr>'; $row2 = '<tr>'; } $i++; } if($i > 0 && $i % 5 !== 1) { $html .= $row1.$row2; } $html .= '</table>'; echo $html;
Comments
Post a Comment