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
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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.