Search code examples
phpcsvfgetcsv

Import specific rows and columns form csv file into table using php


I'm very new to php but have been experimenting fairly successfully with using 'fgetcsv' to bring in a CSV file and convert it into an html table.

However I also have a large CSV file with 70 columns and 700 rows, but I only want to display columns 1 to 47 and rows 3 to 21 in one table, and then same columns but rows 22 to 44 in another table.

I'd appreciate some help with this, below is the code I am currently using:

<?php

$row = 1;
if (($handle = fopen("CSV/test.csv", "r")) !== FALSE) {

    echo '<table border="1">';

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        if ($row == 1) {
            echo '<thead><tr>';
        }else{
            echo '<tr>';
        }

        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />\n";
            if(empty($data[$c])) {
               $value = "&nbsp;";
            }else{
               $value = $data[$c];
            }
            if ($row == 1) {
                echo '<th>'.$value.'</th>';
            }else{
                echo '<td>'.$value.'</td>';
            }
        }

        if ($row == 1) {
            echo '</tr></thead><tbody>';
        }else{
            echo '</tr>';
        }
        $row++;
    }

    echo '</tbody></table>';
    fclose($handle);
}
?>

Solution

  • <?php
    
    $row = 1;
    
    if(($handle = fopen("CSV/test.csv", "r")) !== false) {
    
        $table1 = $table2 = '<table border="1">';
    
        while (($data = fgetcsv($handle, 1000, ",")) !== false) {
    
            $table1Add = $table2Add = false;
            if($row >=3 && $row <= 21)
                $table1Add = true;
            if($row >=22 && $row <= 44)
                $table2Add = true;
    
            $num = count($data);
    
            if($row == 1) {
    
                $table1 .= '<thead><tr>';
                $table2 .= '<thead><tr>';
    
                for($c = 1; $c <= 47; $c++) {
                    $value = empty($data[$c]) ? "&nbsp;" : $data[$c];
    
                    $table1 .= '<th>'.$value.'</th>';
                    $table2 .= '<th>'.$value.'</th>';
                }
    
                $table1 .= '</tr></thead><tbody>';
                $table2 .= '</tr></thead><tbody>';
    
            } else {
    
                if($table1Add) $table1 .= '<tr>';
                if($table2Add) $table2 .= '<tr>';
    
                for($c = 1; $c <= 47; $c++) {
                    $value = empty($data[$c]) ? "&nbsp;" : $data[$c];
    
                    if($table1Add) $table1 .= '<td>'.$value.'</td>';
                    if($table2Add) $table2 .= '<td>'.$value.'</td>';
                }
    
                if($table1Add) $table1 .= '</tr>';
                if($table2Add) $table2 .= '</tr>';
    
            }
    
            $row++;
    
        }
    
        $table1 .= '</tbody></table>';
        $table2 .= '</tbody></table>';
        fclose($handle);
    
        echo $table1;
        echo $table2;
    }
    ?>