Search code examples
phphtmlcsvhtml-tablefgetcsv

How to create dynamic table from both directory and CSV files


I need to create a table whose first column is populated from subdirectory names inside a directory and rest are from a CSV file. This have to be a dynamic table and table headers have to be added from the code. What's wrong with my code?

I am an absolute beginner. So, please ignore my stupidity.

enter image description here

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

Solution

  • <?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>';
             }
          }
       }
    
    
    
    
    ?>