php foreachloop with html tables


php foreachloop with html tables

对于'n'个隔间,我有一个foreach循环。我想在每一行显示4个隔间,并在下一行显示其余隔间。如何在foreach循环中每行限制4个隔间。

现在下面的代码显示所有的格子在一行

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

使用array_chunk PHP Manual获取每行4个值:

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

这应该能达到目的,而且很灵活:

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

试试这个

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

基本技术包括使用数字计数器跟踪列,并使用模数运算符将其保持在列范围内。此外,由于它是一个HTML表,您可能还需要填充缺失的单元格,以便显示效果更好。

下面是一个例子:

<?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;
}