Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从CSV生成HTML表-忽略不需要的列_Php - Fatal编程技术网

Php 从CSV生成HTML表-忽略不需要的列

Php 从CSV生成HTML表-忽略不需要的列,php,Php,我有这段代码,它获取CSV并从中生成HTML表 我有一个名为$idsColumnsWanted的数组,我计划只存储我想在表中显示数据的列 不幸的是,按照它当前的工作方式,在显示列0和1之后,它不会显示任何其他内容。它忽略了之后的一切 我做错了什么 <?php $idsColumnsWanted = array(0,1,16); echo "<table class='table table-bordered'>\n\n"; $f = fopen("users.csv",

我有这段代码,它获取CSV并从中生成HTML表

我有一个名为
$idsColumnsWanted
数组
,我计划只存储我想在表中显示数据的列

不幸的是,按照它当前的工作方式,在显示列0和1之后,它不会显示任何其他内容。它忽略了之后的一切

我做错了什么

<?php

$idsColumnsWanted = array(0,1,16);

echo "<table class='table table-bordered'>\n\n";

$f = fopen("users.csv", "r");

$first_line=false;

while (($line = fgetcsv($f)) !== false) {

    // Restart column index
    $i = 0;

    $row ="";

    if($first_line == false) {
         $row = "<thead><tr>";
         $col= "th";
    }
    else {
         $row = "<tr>";
         $col= "td";
    }


    $is_empty = false;

    foreach ($line as $cell) {

        // Skips all columns not in your list
        if (! in_array($i, $idsColumnsWanted)) continue;

        if ($cell !== '') {
            $row .= "<".$col.">" . htmlspecialchars($cell) . "   </".$col.">";
        } else {
            $is_empty = true;
        }

        // Increase index
        $i++;

    }


    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>";

?>

也许,您可以这样做:

    <?php

        $idsColumnsWanted   = array(0,1,16);
        $output             = "<table class='table table-bordered'>\n\n";
        $f                  = fopen("file.csv", "r");
        $csv                = fgetcsv($f);
        $counter            = 0;

        while (($line = fgetcsv($f)) !== false){
            if($counter == 0){
                // BUILD THE TABLE HEADER
                $output    .= "<thead>\n<tr>" . PHP_EOL;
                foreach($line as $i=>$cell){
                    // SKIP UNWANTED COLUMNS...
                    if (!in_array($i, $idsColumnsWanted)) continue;
                    if(!empty($cell)) {
                        $output .= "<th>{$cell}</th>" . PHP_EOL;
                    }
                }
                // CLOSE THE TABLE HEADER & ITS ROW
                $output    .= "</tr>\n<thead>" . PHP_EOL;
                // ADD THE TABLE BODY TAG
                $output    .= "</tbody>" . PHP_EOL;
            }else{
                // BUILD THE NORMAL TABLE-ROWS
                $output .= "<tr>" . PHP_EOL;
                foreach($line as $i=>$cell){
                    // SKIP UNWANTED COLUMNS...
                    if (!in_array($i, $idsColumnsWanted)) continue;
                    if(!empty($cell)){
                        $output .= "<td>" . htmlspecialchars($cell) . "</td>" . PHP_EOL;
                    }
                }
                $output .= "</tr>" . PHP_EOL;
            }
            $counter++;
        }
        // CLOSE THE TABLE BODY AND THE TABLE ITSELF.
        $output    .= "</tbody>" . PHP_EOL;
        $output    .= "</table>" . PHP_EOL;

        fclose($f);

        // ECHO THE OUTPUT...
        echo $output;
    ?>

在第一次调用
(!in_array($i,$idsColumnsWanted))
后继续,这意味着在这种情况下,
$i
永远不会递增,并且将保留为不应包含的值。为什么不允许
foreach($i=>$cell行)
处理您的列编号吗?@MarkBaker第二个列处理得很好-请添加作为答案&我将绿色勾选。非常感谢您在这里花时间和解释:-)