简体   繁体   中英

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);
}
?>

for the columns try this

for ($c=0; $c < 47; $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>';
        }
    }

So you print cells between column 0 and column 47. For the row you have to set aa counter, and, inside the while statement, use an if to print the rows in the range that you need. It's better that you set an exit condition in while to exit after row 44 if you dont need the others.

<?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;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM