如何从目录和 CSV 文件创建动态 table

How to create dynamic table from both directory and CSV files

我需要创建一个 table,其第一列由目录内的子目录名称填充,其余列来自 CSV 文件。这必须是动态的 table 并且 table headers 必须从代码中添加。我的代码有什么问题?

我绝对是初学者。所以,请忽略我的愚蠢。

$dir = 'D:\workspace';
$dirlist = preg_grep('/^([^.])/', scandir($dir)); 
$row = 1;
if (($handle = fopen("D:\workspace\demo\database.csv", "r")) !== FALSE) {

echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";

while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
    $num = count($data);
    if ($row == 1) {
        echo '<thead><tr>';
    }else{
        echo '<tr>';
    }

    for ($c=0; $c < $num; $c++) {

        if(empty($data[$c])) {
            $value = "&nbsp;";
        }else{
            $value = $data[$c];

        }
        if ($row == 1) {
            echo '<th>'.$value.'</th>';
        }else{
            foreach ($dirlist as $rowdirectory)
            {
                echo '<td>' . $rowdirectory . '</td>';
                echo '<td>'.$value.'</td>';

            }
        }

    }

    if ($row == 1) {
        echo '</tr></thead><tbody>';
    }else{
        echo '</tr>';
    }
    $row++;
}

echo '</tbody></table>';
fclose($handle);
}
<?php
#------------------------------------------Function for Reading Directory-------------------------------------------
function readdirectory($dir)
{
    $dirlist = preg_grep('/^([^.])/', scandir($dir));  // for all(./../anything that 
           starts with .)
    //$dirlist = preg_grep('/[^.]/', scandir($dir));   //only . & ..
    //$dirlist =preg_grep('/^[(^.)]/', scandir($dir));//only files that starts with .

    return $dirlist;

}

#------------------------------------------Function for Reading CSV file-------------------------------------------
function readcsvfile($source)
{
   $handle = fopen($source, "r");
   $filecontent = null;
   while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
   {
      $filecontent[] = $data;
   }
   fclose($handle);
   return $filecontent;

}

$filecontent= readcsvfile("D:\workspace\demo\database2.csv");

#-------------------Directory Function calling, header array cration and other declaration------------------------------------

$conf_prefix= "../";
$conf_suffix="index.php";


$headerarray= $filecontent[0];
#var_dump($headerarray);
$headersize= count($headerarray);




echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
$dir = 'D:\workspace';
$dirlist= readdirectory($dir);

#------------------------------------------Creating 1st row/Header row of Table-------------------------------------------

echo '<tr>';
for($a=0; $a<$headersize;$a++)
{
    echo '<td>'.$headerarray[$a].'</td>';
}
 echo '</tr>';

 #-----------------------------Creating Table elements by comparing both directory arrays and CSV file array-------------------------------------------

 foreach ($dirlist as $rowdirectory)
 {
      foreach ($filecontent as $value) {
           $num = count($value);

           if ($rowdirectory== $value[0])

           {

               $link= $conf_prefix. $rowdirectory."/".$conf_suffix;

               echo '<tr>';
               echo '<td> <a href="'.$link.'">' . $rowdirectory . '</a> </td>';
               for( $c=1; $c < $num; $c++) {// loop for csv file
                {
                     echo '<td>'.$value[$c].'</td>';//else print "value"
                }
            }


              echo '</tr>';
         }
      }
   }




?>