Skip to content
Advertisement

How to display all .csv content from a folder into a single HTML Table or Datatable in PHP or Ajax

I have updated my question post with the code that currently have. Hope someone can help me.

I can’t seem to figure out how to put all the values from the CSV to an HTML table

<?php
$files = glob("./data/*.csv");
foreach($files as $file) {
    if (($handle = fopen($file, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
            echo implode("t", $data);
        }
        echo "<br>";
        fclose($handle);
    } else {
        echo "Could not open file: " . $file;
    }
}
?>

Advertisement

Answer

<?php
$files = glob("./data/*.csv");
$display_csv = array();
foreach($files as $file) {
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$xyz = array('company_name'=>$data[0],'case_id'=>$data[1]);
array_push($display_csv, $xyz);
}
fclose($handle);
}else 
{
echo "Could not open file: " . $file;
}
}
echo "<table class='table table-dark table-striped' id='call_csv' border='2'><thead><tr><th>Company Name</th><th>Case ID</th></tr></thead><tbody>";
foreach($display_csv as $row){
echo "<tr><td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['case_id'] . "</td></tr>";
}
echo "</tbody></table>";
?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement