I am building a table from a csv. The csv format is unknown when I open it so I don't know how many columns it will build.
My code is the following and works flawlessy:
$row = 1;
$size = 0;
$file = "FileCsv.csv";
if (($handle = fopen($file, "r")) !== FALSE) {
echo '<table class="table table-bordered">';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$array_riga=explode(';',$data[$c]);
$length = count($array_riga);
if($length>$size){$size=$length;}
if($length>1){
echo '<tr><td>'.$row.'</td>';
foreach($array_riga as $cell ){
echo '<td>'.$cell.'</td>';
}
echo '</tr>';
$row++;
}
}
}
echo '</table>';
echo 'column number: '.$size+1; //aggiungo la colonna che ho creato io virtualmente
fclose($handle);
}
Only at the end of this code I'll know how many columns do I have. What I'd like to do is to add a row to the table with some information inside. The row should be the first row of the table. If i knew the maximum number of columns I'd just add it before starting the while
but I know it only at the end of the while: The number of columns may vary per row (usually will increase after the first few rows that are header rows)
I know I can add it with js when the table is rendered but can I do it with PHP somehow?.
These csv files are from different sources and there can be headers or other contents that can vary the number of columns.
question from:
https://stackoverflow.com/questions/65904319/prepend-a-table-row-to-a-created-table 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…