3

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

    }
0

7 Answers 7

4

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>';
Sign up to request clarification or add additional context in comments.

Comments

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

7 Comments

assuming $num is numeric... and gap-less
and start with 0 plus an empty row at the top.
(1) The first iteration would generate an extra blank row. You'd have to see if $num%4 == 3 (rather than 0) in order to make it work.
(2) What Yanick said. :) Though an array of records will often use sequential numeric keys, the few times it doesn't will leave you wondering WTF is messing up your tables.
@Yanick, yes, but something tells me $Cubicle is not an associative array... we'll se if i was right :)
|
1

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

Comments

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

    }

Comments

1

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

}

1 Comment

this prints all cubicles in one coloumn only :(
1
print '<table border="2">';
print '<tr>';
$cellIndex = 0;
foreach($Cubicle as $cubicle )
{
   if ((++$cellIndex % 4) == 0) {
      print '</tr><tr>';
   }
   print '<td>';
   ...

2 Comments

using this i get empty row balnk :(
the test may have needed to be wrapped in parenthesis, I modified the answer to take into account that maybe the test was trying if (val % 0). In any case, depending on the array indexes is not a good idea, since PHP arrays don't have to have contiguous key indexes...
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.