etocaj Posted February 18, 2010 Share Posted February 18, 2010 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. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 18, 2010 Share Posted February 18, 2010 $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></tr>"; } HTH Teamatomic } Quote Link to comment Share on other sites More sharing options...
etocaj Posted February 18, 2010 Author Share Posted February 18, 2010 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! Quote Link to comment Share on other sites More sharing options...
yozyk Posted February 18, 2010 Share Posted February 18, 2010 echo '<table border=1>'; $handle = fopen("file.csv", "r"); while (($r = fgetcsv($handle, 1000, ';')) !== FALSE) echo '<tr><td>',$r[0],'</td><td>',$r[1],'</td><td>',$r[2],'</td><td>',$r[7],'</td></tr>'; fclose($handle); echo '</table>'; Quote Link to comment Share on other sites More sharing options...
etocaj Posted February 18, 2010 Author Share Posted February 18, 2010 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! Quote Link to comment Share on other sites More sharing options...
etocaj Posted February 18, 2010 Author Share Posted February 18, 2010 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.