从PHP回应制作HTML友好数据

时间:2014-05-27 06:35:44

标签: php html export-to-csv

我一直在使用PHP脚本来读取contact.csv文件。我想在这张桌子上应用更好的造型。有没有办法编辑这个脚本以使其更友好?

$row = 1;
if (($csvFile = fopen("contacts.csv", "r")) !== FALSE) {
    $i = 0;
    $wantedColumns = array(1,3,57);
    echo "<table border='1' cellpadding='0' cellspacing='0'>";
    while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        echo "<tr>";
        for ($c=0; $c < $num; $c++) 
        {
            if (!in_array($c,$wantedColumns)) continue;
            echo "<td>$data[$c]</td>";
        }
        echo "</tr>";
    }
    fclose($csvFile);
}

我尝试将其嵌入到html中,但echo "<td>$data[$c]</td>";将其抛弃。

1 个答案:

答案 0 :(得分:0)

将一行中的所有html内容连接起来,然后echo连接它,否则它不会有效,因为某些标记需要关闭</>标记,其中包含一个<>标记$html = "<table border='1' cellpadding='0' cellspacing='0'>"; while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) { $num = count($data); $row++; $html .= "<tr>"; for ($c=0; $c < $num; $c++) { if (!in_array($c,$wantedColumns)) continue; $html .= "<td>$data[$c]</td>"; } $html .= "</tr>"; } $html .= "</table>"; echo $html; 1}}:

{{1}}