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
Loop inside the set
– Kleber Souza
@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).
– Bacco
@Klebersouza it is, but how to do it?
– Filipe
@Bacco I’m editing, I’ll throw everything there! Thanks
– Filipe
@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.– Bacco