Logic PHP: how to build this looping (for, while, foreach)?

Asked

Viewed 452 times

2

Good night.

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

What I’m picking 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 and so forth.

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

Below is my code. NOTE: I am using 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

  • 1

    Loop inside the set

  • @Klebersouza doesn’t want to post an example as an answer? (although the question is ambiguous, there is a risk that this is not what the author wants).

  • @Klebersouza it is, but how to do it?

  • @Bacco I’m editing, I’ll throw everything there! Thanks

  • @Filipe posted a reply of general use, but if you post excerpt of a print_r( $return['Sheet0'] ) can update in response to the real case. No need to post the whole result, only what corresponds to a row and a half of sheet data.

2 answers

3


Follow an example of loop chained, 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 on IDEONE.

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


This answer basically applies the suggestion of Kleber Souza, posted in a comment.

0

An interesting way to do using recursiveness 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 and varied levels, without needing to modify the code or specify number of columns and rows.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.