phphtmlcsvtable-footer

Adapt existing script for converting csv to html table to include <tfoot>


I've used the code below for several years to convert selected data in a large csv file into an html table. It selects a specific range of rows and columns to present.

Is it possible to add a command to wrap the last four selected rows in a tfoot tag? So in this example, rows 167-170. Similar to row 147 being wrapper in a thead.

<?php
        $row = 1;
        
        if(($handle = fopen("CSV/thesfpsquads23.csv", "r")) !== false) {
        
            $table1 = '<table id="exampleaugnov" class="sfpsquad augnov">';
        
            while (($data = fgetcsv($handle, 1000, ",")) !== false) {
        
                $table1Add = false;
                if($row >=147 && $row <= 170)
                    $table1Add = true;
        
                $num = count($data);
        
                if($row == 147) {
        
                    $table1 .= '<thead><tr>';
        
                    for($c = 0; $c <= 21; $c++) {
                        $value = empty($data[$c]) ? "0" : $data[$c];
        
                        $table1 .= '<th>'.$value.'</th>';
                    }
        
                    $table1 .= '</tr></thead><tbody>';
        
                } else {
        
                    if($table1Add) $table1 .= '<tr>';
        
                    for($c = 0; $c <= 21; $c++) {
                        $value = empty($data[$c]) ? "0" : $data[$c];
        
                        if($table1Add) $table1 .= '<td>'.$value.'</td>';
                    }
        
                    if($table1Add) $table1 .= '</tr>';
        
                }
        
                $row++;
        
            }
        
            $table1 .= '</tbody></table>';
            fclose($handle);
        
            echo $table1;
        }
        ?>

Solution

  • Well, this code can be improved, try using an AI, but let's just get it working, hopefully. Row 140 - inside <thead> Row 167-170 inside <tfoot> in between in <tbody> and other rows discarded.

    $row = 0;
    if (($handle = fopen("CSV/thesfpsquads23.csv", "r")) !== false) {
        $table1 = '<table id="exampleaugnov" class="sfpsquad augnov">';
        while (($data = fgetcsv($handle, 1000, ",")) !== false) {
            $row++;
            if ($row < 147 || $row > 170) {
                continue;
            }
            $num = count($data);
            if ($row == 147) {
                $table1 .= '<thead><tr>';
                for ($c = 0; $c <= 21; $c++) {
                    $value = empty($data[$c]) ? "0" : $data[$c];
                    $table1 .= '<th>' . $value . '</th>';
                }
                $table1 .= '</tr></thead><tbody>';
            } else {
                if ($row == 167) {
                    $table1 .= '</tbody><tfoot>';
                }
    
                $table1 .= '<tr>';
                for ($c = 0; $c <= 21; $c++) {
                    $value = empty($data[$c]) ? "0" : $data[$c];
                    $table1 .= '<td>' . $value . '</td>';
                }
                $table1 .= '</tr>';
    
                if ($row == 170) {
                    $table1 .= '</tfoot>';
                }
            }
        }
        $table1 .= '</table>';
        fclose($handle);
        echo $table1;
    }