通过php使用CSV数据的表中的样式列

时间:2012-03-11 07:07:31

标签: php html csv

我希望设置我的表格列的样式,例如make作者列显示例如“书籍标题”用粗体红色,而价格用斜体蓝色等...

2012-03-11,paul,the book title,386,10,256

我的PHP代码

echo "<table cellspacing='0' cellpadding='0'>   \n\n
<thead>
<tr>
<th>Date</th>
<th>Author</th>
<th>Title</th>
<th>Prices</th>
<th>Sales</th>
<th>Rank</th>
</tr>
</thead>";
$f = fopen("local/bookdata.csv", "r");
$i = 0;

while (($line = fgetcsv($f)) !== false) {
     echo "<tr class='$class".(($i%2)?'odd':'even')."'>";
        foreach ($line as $cell) {
                echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>\n";
    $i++;
}

fclose($f);
echo "\n</table>";

欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

PHP

$classes = array('', '', 'title', 'price' , '', '');
// ...
foreach ($line as $idx=>$cell) {
    echo "<td class='{$classes[$idx]}'>". htmlspecialchars($cell) . "</td>";
}

CSS

td.title { font-weight: bold; color: red; }
td.price { font-style: italic; color: blue;  }