php foreachloop与html表

时间:2011-07-15 11:37:55

标签: php http foreach

我有一个'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>';

    }

7 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

这应该可以解决问题,而且很灵活:

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

答案 4 :(得分:1)

试试这个。

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

}

答案 5 :(得分:1)

基本技术包括使用数字计数器跟踪列和模数运算符以使其保持在列范围内。此外,由于它是一个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;
}

答案 6 :(得分:1)

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

    }