Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

php - prepend a table row to a created table

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One solution would be to use a simple buffer to store the output until you're ready to render it.

  • $begin: Stores table opening tag.
  • $end: Stores table closing tag
  • $buffer: Stores the output of your old "echo" statements and builds the rows within your while/for loops.

The final line assembles these together, and inserts your count row before any other rows from $buffer.

Adjust as needed.

$row = 1;
$size = 0;
$file = "FileCsv.csv";
$begin = "";
$buffer = "";
$end = "";

if (($handle = fopen($file, "r")) !== FALSE) {
    $begin .= '<table class="table table-bordered">';

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        // $buffer.= "<p> $num fields in line $row: <br /></p>
";
        
        for ($c=0; $c < $num; $c++) {
            $array_riga=explode(';',$data[$c]);
            $length = count($array_riga);
            if($length>$size){$size=$length;}
            if($length>1){
                $buffer .= '<tr><td>'.$row.'</td>';
                foreach($array_riga as $cell ){
                    $buffer .= '<td>'.$cell.'</td>';
                }
                $buffer .= '</tr>';
                $row++;
            }
        }
    }
    $end .= '</table>';
    $end .= 'column number: '.$size+1; //aggiungo la colonna che ho creato io virtualmente
    fclose($handle);
}

// I'm using str_repeat to flesh out the rest of the TDs according to your column account. 
// This allows us to create a proper first row with the count of rows. 
// This prevents a misaligned table. Fix as needed. 

$count_row = "<tr><td>$row</td> ". str_repeat("<td></td>", $length) . "</tr>";
echo $begin . $count_row . $buffer . $end; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...