PHP logic: how to construct this looping (for, while, foreach)?

1

Good evening.

I'm in the following situation: I'm reading an XLS and playing in a table. This XLS has 52 rows and 6 columns.

What I'm getting caught up to do is:

$result[linha 1][coluna A]
$result[linha 1][coluna B]
$result[linha 1][coluna C]
$result[linha 1][coluna D]
$result[linha 1][coluna E]
$result[linha 1][coluna F]

$result[linha 2][coluna A]
$result[linha 2][coluna B]
$result[linha 2][coluna C]
$result[linha 2][coluna D]
$result[linha 2][coluna E]
$result[linha 2][coluna F]

$result[linha 3][coluna A]
$result[linha 3][coluna B]
$result[linha 3][coluna C]
$result[linha 3][coluna D]
$result[linha 3][coluna E]
$result[linha 3][coluna F]

And so on.

How do I compose this loop? I'm trying with for and while, but it's not rolling. I thought about doing with foreach, but I do not think this option will do. I'm really lost on how to execute this loop inside loop.

Below is my code. NOTE: I am using the PHPExcel lib to read:

<?php

    error_reporting(E_ALL);

    ini_set('display_errors', TRUE);

    ini_set('display_startup_errors', TRUE);

    define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

    require_once dirname(__FILE__) . '/inc/PHPExcel.php';

    $fileName = 'file.xls';

    $excelReader = PHPExcel_IOFactory::createReaderForFile($fileName);

    $excelReader->setReadDataOnly();

    $excelReader->setLoadAllSheets();

    $excelObj = $excelReader->load($fileName);

    $excelObj->getActiveSheet()->toArray(null, true,true,true);

    $worksheetNames = $excelObj->getSheetNames($fileName);

    $return = array();

    foreach( $worksheetNames as $key => $sheetName ){

        $excelObj->setActiveSheetIndexByName($sheetName);

        $return[$sheetName] = $excelObj->getActiveSheet()->toArray(null, true,true,true);
    }

    $totalRows  = count($return['Sheet0']);
    $columns    = range('A','F');
    $colNum     = 0;
    $rowNum     = 1;
    $maxCol     = 1;

    echo '<table class="table table-responsive">';

    echo '<thead>';
    echo '<th>';

        while ( $colNum < count($columns) ) {

            echo '<td>' . $return['Sheet0'][11][ $columns[ $colNum ] ] . '</td>';
            $colNum++;

        }

    echo '<th>';
    echo '</thead>';


    echo '<tbody>';
    echo '<tr>';

    for ( $a=1; $a<=$totalRows; $a++ ) {

        if ( $rowNum <= 6 ) {

            echo  "return['Sheet0'][". $a . "][columns[" . $rowNum . "] ] " . EOL;
            //echo '</tr><tr>';

        }

        $rowNum++;
    }


    echo '</tr>';
    echo '</tbody>';

    echo '</table>';

Thank you

    
asked by anonymous 03.01.2016 / 04:27

2 answers

2

Here is an example of looped loop , already mounting an HTML table:

echo "<table>\n";
for ( $row = 1; $row <= $totalRows; ++$row ) {
   echo '<tr>';
   for ( $col = 1; $col <= $totalCols; ++$col ) {
      echo '<td> linha '.$row.', coluna '.$col.'</td>';
   }
   echo "</tr>\n";
}
echo "</table>\n";

See working at IDEONE .

It's just a matter of adapting to what you're going to do with the data.


This answer basically applies to Kleber Souza's suggestion, posted in a comment.

    
03.01.2016 / 05:03
0

An interesting way to do this using recursion with anonymous function:

$arr['linha 1']['coluna 1'] = 11;
$arr['linha 1']['coluna 2'] = 12;
$arr['linha 2']['coluna 1'] = 21;
$arr['linha 2']['coluna 2'] = 22;
$arr['linha 3']['coluna 1'] = 31;
$arr['linha 3']['coluna 2'] = 32;

$foo = function($arr, $key = 'row') use (&$foo){
    $rs = '';
    foreach ($arr as $k => $v)
        $rs .= (($key == 'row')? '<tr>'.$foo($v, 'col').'</tr>' : '<td>'.$v.'</td>');
    return $rs;
};

echo '<table border="1">'.$foo($arr).'</table>';

This example is for 2-level array. However it is possible to adapt it to identify multiple levels and vary, without having to modify the code or to specify quantity of columns and lines.

    
03.01.2016 / 08:48