I have a foreach loop for 'n' number of cubicles. I want to display 4 cubicles in each row and reaminig in next row. How to limit 4 cubicles in each row within foreach loop.
Right now below code display all cubicles in one row
print '<table border="2">';
print '<tr>';
foreach($Cubicle as $cubicle )
{
print '<td>';
if($nodeStatus == '0'){
printf('<a href= "#" style="color: green; font-weight:bold" onClick="showDetails(\'%s\');">%s</a> ', $nodeId,$insert);
}
elseif($nodeStatus == '1'){
printf('<a href= "#" style="color: #AF7817; font-weight:bold" onClick="showDetails(\'%s\');">%s</a> ', $nodeId,$insert);
}
else{
printf('<a href= "#" style="color: RED; font-weight:bold" onClick="showDetails(\'%s\');">%s</a> ', $nodeId,$insert);
}
print '</td>';
}
Use array_chunk PHP Manual to obtain the 4 values per each row:
echo '<table border="2">';
foreach(array_chunk($Cubicle, 4) as $row )
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>', $col /* your column formatting */, '</td>';
}
echo '</tr>';
}
echo '</table>';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With