繁体   English   中英

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

[英]How to create dynamic table from both directory and CSV files

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

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

在此输入图像描述

$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>';
         }
      }
   }




?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM