【发布时间】:2016-09-04 06:57:53
【问题描述】:
我正在使用以下代码从 CSV 文件生成 HTML 表格。
我只想显示具有以下索引的列:
$idsColumnsWanted = array(0,1,19,16);
如何在我现有的代码中使用它?
echo "<table class='table table-bordered'>\n\n";
$f = fopen("users.csv", "r");
$first_line=false;
while (($line = fgetcsv($f)) !== false) {
$row ="";
if($first_line == false) {
$row = "<thead><tr>";
$col= "th";
}
else {
$row = "<tr>";
$col= "td";
}
$is_empty = false;
foreach ($line as $cell) {
if ($cell !== '') {
$row .= "<".$col.">" . htmlspecialchars($cell) . "</".$col.">";
} else {
$is_empty = true;
}
}
if($first_line == false) $row .= "</tr></thead>";
else $row .= "</tr>";
$first_line=true;
if ($is_empty) {
continue;
} else {
echo $row;
}
}
fclose($f);
echo "\n</table>";
【问题讨论】: