表中的fgetcsv获取额外的单元格

时间:2017-07-27 10:19:09

标签: php csv

我在表格中使用了fgetcsv,除了最后的额外单元格外,它工作正常。 见http://jobbel.nl/csv.php。我该如何解决这个问题?

这是我的剧本:

    <table>
    <tr>
        <th>Datum</th>
        <th>Tijd</th>
        <th>Temperatuur</th>
    </tr>
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<tr><td>";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "</td><td>";
        }
        echo "</td></tr>";
    }
    fclose($handle);
}
?>
</table>

2 个答案:

答案 0 :(得分:0)

你回应细胞有点奇怪 将其更改为仅在实际需要时回显<td>

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<tr>";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo "<td>" . $data[$c] . "</td>";
    }
    echo "</tr>";
}

使用implode()有另一种解决方案,它有点清洁:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    echo "<tr><td>" . implode('</td><td>', $data) . "</td></tr>";
}

答案 1 :(得分:0)

这应该是......

                    <?php

                    $file = file('templog.csv');
                    foreach($file as $line){
                        list($datum,$tijd,$temparatuur)=explode(",", $line);
                        echo "<td>$Date";
                        echo "<td>$Time";
                        echo "<td>$Temparature<tr>";
                    }
                    ?>