Jump to content

CSV File to HTML Table Script Help


etocaj

Recommended Posts

I have a basic script that I'm using to display raw data from a csv file on a website:

 

<?php
   $file = "myfile.csv";
   $fh = fopen($file, "rt");
   $userdata = fread($fh, filesize($file));
   fclose($fh);

   echo $userdata; 
?>

 

 

The csv file header looks like this:

 

Date/Time,MPSAS,NELM,SerialNo,Protocol,Model,Feature,Temp©

 

What I need is a simple four column table with Data/Time, MPSAS, NELM, and Temp © displayed. The other columns are not needed. 

 

I have searched the forum for an answer but to no avail.

 

Any help would be greatly appreciated!

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/192472-csv-file-to-html-table-script-help/
Share on other sites

Great, that worked, except columns 1,2, and 7 were all overlapped on one another.

 

I simply changed this line by adding <table></table>

echo "[b]<table>[/b]<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></tr>[b]</table>[/b]";

 

It looks pretty good now, maybe just some tweaking to the table spacing, etc.

 

Thanks for the help!

This is the final code I used to get the table to format correctly:


echo "<table align='left' width='685' bordercolor='ccc' border='1'>";

$file = "myfile.csv";

$content=file("$file");

foreach($content as $line)
{

$row=explode(",",$line);

echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";
}

echo "</table>";

?>

 

 

I realized that this script will not update again on a browser refresh for some reason. I copied it into a page as a part of a wordpress theme, but the table did not update. The csv file is automatically sent to my web hosting server every minute so i know that it should include new data.

 

Is there something else that needs to be included in the code to get this script to run again on a browser refresh? I know it is not a cache issue since I cleared it out. I also looked at the page on a different computer.

 

Also, is it possible to have the table show the most recent data first? As it is now, you would have to scroll down the table to see the latest readings.

 

Again, thanks for your help with this script!

Looks like the script is indeed updating properly. I tried the site from another computer at work and the data is changing on a refresh.

 

I'm using array_multisort to try and get the most recent data to show at the top, but with no luck.

 

Here is the code:

 

echo "<table align='left' width='685' bordercolor='ccc' border='1'>"; 
$file = "myfile.csv"; 
$content=file("$file"); 
foreach($content as $line){$row=explode(",",$line); 
array_multisort($row[0], SORT_ASC, $content); 
echo "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";} 
echo "</table>";?>

 

I have never used array_multisort before so I'm stuck at this point.

 

Anyone have any ideas?

 

Thanks!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.