开发者

php foreachloop with html tables

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="show开发者_如何学运维Details(\'%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>';


 print '<table border="2">';
 print '<tr>';
 foreach($Cubicle as $num => $cubicle )
 {
   if ($num%4 == 0)
   {
     print '</tr><tr>';
   }
   print '<td>';
   ...


This should do the trick, and is flexible:

function printTable($cubicles, $items_per_row) {
    print '<table border="2">';
    while($row = array_splice($cubicles, 0, $items_per_row)) {
        print '<tr>';
        printRow($row, $items_per_row);
        print '</tr>';
    }
    print '</table>';
}

function printRow($cubicles, $items_per_row) {
    for($i=0; $i<$items_per_row; $i++) {
        print '<td>';
        print (isset($cubicles[$i]) ? $cubicles[$i] : '&nbsp;');
        print '</td>';
    }
}

printTable($Cubicle, 4);


 print '<table border="2">';
 print '<tr>';
 $rowNum = 0;
 foreach($Cubicle as $cubicle){
     $rowNum++;

     if($rowNum % 4 == 0){ echo '<tr>'; }

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

     if($rowNum % 4 == 0){ echo '</tr>'; }

    }


try this.

print '<table border="2">';

$s=0;
foreach($Cubicle as $cubicle )
{
    if($s == 0){
        echo $open_tr = '<tr>';
    }else if($s % ceil(count($Cubicle)/4) == 0){
        echo $open_ul = '</tr><tr>';
    }else{
        echo $open_ul = '';
    }
    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>';

    if($s == (count($Cubicle) - 1)){
        echo '</tr>';
        $s++;
    }

}


print '<table border="2">';
print '<tr>';
$cellIndex = 0;
foreach($Cubicle as $cubicle )
{
   if ((++$cellIndex % 4) == 0) {
      print '</tr><tr>';
   }
   print '<td>';
   ...


The basic technique consist on using a numeric counter to keep track of the column and the modulus operator to keep it in within the column range. Also, since it's an HTML table you may also want to fill missing cells so the display looks good.

Here's an example:

<?php

define('NUM_COLUMNS', 4);

$cubicle = array('A', 'B', 'C', 'D', 'E', 'F');

if( empty($cubicle) ){
    echo '<p>No cubicles found.</p>';

}else{
    echo '<table>' . PHP_EOL;

    $column = 0;
    foreach($cubicle as $cubicle_name){
        if( $column==0 ){
            echo '<tr>';
        }

        echo '<td>' . htmlspecialchars($cubicle_name) . '</td>';

        if( $column==NUM_COLUMNS-1 ){
            echo '</tr>' . PHP_EOL;
        }

        $column = ($column+1) % NUM_COLUMNS;
    }

    // Fill gaps
    if( $column>0 ){
        while( $column<NUM_COLUMNS ){
            echo '<td>—</td>';
            $column++;
        }
        echo '</tr>' . PHP_EOL;
    }

    echo '</table>' . PHP_EOL;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜