Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php foreachloop with html tables

Tags:

http

foreach

php

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

    }
like image 859
Vinod HC Avatar asked May 11 '26 17:05

Vinod HC


1 Answers

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>';
like image 182
hakre Avatar answered May 13 '26 07:05

hakre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!