Jump to content

External realtime


Secondlaw

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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>";
?>

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.