Secondlaw Posted June 25, 2007 Share Posted June 25, 2007 Hi, I was hoping someone can help me out here. I have a piece of PHP code which kind of does what I need. Here is the code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> <html> <head> <title>North Eastern Fly Fishing - USGS @ Hale Eddy</title> <style type="text/css"> <!-- table{ background-color: Black; } tbody{ background-color: Aqua; } thead{ background-color: Yellow; } td{ padding: .4em; } .style1 { font-family: Arial, Helvetica, sans-serif; font-size: 18px; font-weight: bold; color: #99CC00; } --> </style> </head> <body> <p class="style1">Delaware River at Hale Eddy - 01426500</p> <table> <thead> <tr> <td>Date</td> <td>Time</td> <td></td> <td>CFS</td> <td>Temp</td> </tr> </thead> <tbody> <?php /* Get contents of file */ $content=file("http://nwis.waterdata.usgs.gov/nwis/uv?cb_00065=on&cb_00010=on&cb_00060=on&format=rdb&period=1&site_no=01426500"); /* Go through each line in turn */ foreach($content as $line) { /* Check that the row of data begins with USGS. You may need to tweek this for other file formats */ if( substr($line, 0, 17)=="01426500" ) { echo "<tr>"; $column = explode("\t", $line); // Explode line based on data fields being seperated by a TAB echo "<td>".date("j F Y" ,strtotime($column[2]))."</td>"; // Make pretty looking date echo "<td>".date("H:i:s" ,strtotime($column[2]))."</td>"; // Make pretty looking time echo "<td>".$column[4]."</td>"; echo "<td>".$column[5]."</td>"; echo "<td>".$column[7]."</td>"; echo "</tr>"; } } ?> </tbody> </table> </body> </html> you can see the output here: http://www.njflyfishing.com/USGS/usgsHale2.php What I'd really like to do is get the very last line of this output and import it into my MySQL database. Then I can run queries on my Database once an hour and have this realtime data on my website. The data I'm aqcuiring is public access so no violations are here. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Lumio Posted June 25, 2007 Share Posted June 25, 2007 Make this: <?php foreach($content as $line) { /* Check that the row of data begins with USGS. You may need to tweek this for other file formats */ if( substr($line, 0, 17)=="01426500" ) { echo "<tr>"; $column = explode("\t", $line); // Explode line based on data fields being seperated by a TAB echo "<td>".date("j F Y" ,strtotime($column[2]))."</td>"; // Make pretty looking date echo "<td>".date("H:i:s" ,strtotime($column[2]))."</td>"; // Make pretty looking time echo "<td>".$column[4]."</td>"; echo "<td>".$column[5]."</td>"; echo "<td>".$column[7]."</td>"; echo "</tr>"; } } ?> to this: <?php $last_line = -1; foreach($content as $i => $line) { /* Check that the row of data begins with USGS. You may need to tweek this for other file formats */ if( substr($line, 0, 17)=="01426500" ) { echo "<tr>"; $column = explode("\t", $line); // Explode line based on data fields being seperated by a TAB echo "<td>".date("j F Y" ,strtotime($column[2]))."</td>"; // Make pretty looking date echo "<td>".date("H:i:s" ,strtotime($column[2]))."</td>"; // Make pretty looking time echo "<td>".$column[4]."</td>"; echo "<td>".$column[5]."</td>"; echo "<td>".$column[7]."</td>"; echo "</tr>"; $last_line = $i } } echo "<tr>"; echo "<td colspan=\"5\"><b>Last line:</b></td>"; echo "</tr>"; $column = explode("\t", $line[$last_line]); echo "<tr>"; echo "<td>".date("j F Y" ,strtotime($column[2]))."</td>"; // Make pretty looking date echo "<td>".date("H:i:s" ,strtotime($column[2]))."</td>"; // Make pretty looking time echo "<td>".$column[4]."</td>"; echo "<td>".$column[5]."</td>"; echo "<td>".$column[7]."</td>"; echo "</tr>"; ?> Quote Link to comment Share on other sites More sharing options...
Secondlaw Posted June 25, 2007 Author Share Posted June 25, 2007 Doesn't work. Thanks for taking a stab at it. Quote Link to comment Share on other sites More sharing options...
Lumio Posted June 25, 2007 Share Posted June 25, 2007 Doesn't work. Thanks for taking a stab at it. For the next time, please give us errormessages. But I think I found the problem: <?php $last_line = -1; foreach($content as $i => $line) { /* Check that the row of data begins with USGS. You may need to tweek this for other file formats */ if( substr($line, 0, 17)=="01426500" ) { echo "<tr>"; $column = explode("\t", $line); // Explode line based on data fields being seperated by a TAB echo "<td>".date("j F Y" ,strtotime($column[2]))."</td>"; // Make pretty looking date echo "<td>".date("H:i:s" ,strtotime($column[2]))."</td>"; // Make pretty looking time echo "<td>".$column[4]."</td>"; echo "<td>".$column[5]."</td>"; echo "<td>".$column[7]."</td>"; echo "</tr>"; $last_line = $i; //here was the problem: I forgot the ; } } echo "<tr>"; echo "<td colspan=\"5\"><b>Last line:</b></td>"; echo "</tr>"; $column = explode("\t", $line[$last_line]); echo "<tr>"; echo "<td>".date("j F Y" ,strtotime($column[2]))."</td>"; // Make pretty looking date echo "<td>".date("H:i:s" ,strtotime($column[2]))."</td>"; // Make pretty looking time echo "<td>".$column[4]."</td>"; echo "<td>".$column[5]."</td>"; echo "<td>".$column[7]."</td>"; echo "</tr>"; ?> Quote Link to comment Share on other sites More sharing options...
Secondlaw Posted June 28, 2007 Author Share Posted June 28, 2007 Thanks, but that produces the same results as the first example I gave. Quote Link to comment Share on other sites More sharing options...
Lumio Posted June 28, 2007 Share Posted June 28, 2007 That cannot be, because I produce another line. Can you please post the html-source? 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.