PHP Parsing method needed.

Hi,
I run a website and have been trying to parse out data from another page.  Here is what I would like to do vs. what I have been able to accomplish...

1.  Click the following link:
http://waterdata.usgs.gov/nwis/uv?cb_00010=on&cb_00060=on&format=rdb&period=1&site_no=01426500

Scroll down a bit until you see:

USGS  01426500....etc...

What I would like to achieve is to get the last entry and the last 5 entries in that list and be able to place it into a webpage.

Here is what I have so far:  http://www.njflyfishing.com/USGS/usgsHale.php   (just view source to see the code).

I can't figure out how to eliminate all but the last entry or all but the last 4-5 entries...

Thanks for your help!




ParaleptropyAsked:
Who is Participating?
 
RoonaanCommented:
Try using this:


<?php
$last_line = -1;
$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");

$content = array_reverse($content);
$lines_to_display = 5;

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, 4)=="USGS")
      {
            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;
         $lines_to_display--;
         if($lines_to_display == 0) {
            break;
         }
      }
}

?>

Kind regards

-r-
0
 
RoonaanCommented:
Hi,

Running view-source on a processed php page does not reveal it's php code.

You can however parse all lines into an array, then use array_slice to remove all put the last five element. Or run array_reverse and only display the first 5.

-r-
0
 
ParaleptropyAuthor Commented:
Yes, you are correct about not revealing php code.  Here is the code I already have:

<!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
$last_line = -1;
$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");

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, 4)=="USGS" )
      {
            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;
      }
}

?>
      </tbody>
</table>

</body>
</html>


http://waterdata.usgs.gov/ny/nwis/current?multiple_site_no=01417500%0D%0A01423000%0D%0A01426500%0D%0A01427000%0D%0A01420980%0D%0A01420500%0D%0A01427510&search_site_no_match_type=exact&index_pmcode_STATION_NM=1&index_pmcode_DATETIME=2&index_pmcode_00065=&index_pmcode_00060=4&index_pmcode_MEAN=&index_pmcode_MEDIAN=&index_pmcode_00010=5&index_pmcode_00011=&index_pmcode_00020=&index_pmcode_00021=&index_pmcode_00035=&index_pmcode_00036=&index_pmcode_00052=&index_pmcode_00055=&index_pmcode_00095=&index_pmcode_61727=&index_pmcode_61728=&index_pmcode_61729=&index_pmcode_62603=&index_pmcode_62607=&index_pmcode_62608=&index_pmcode_62619=&index_pmcode_70969=&index_pmcode_72019=&index_pmcode_72020=&index_pmcode_74207=&index_pmcode_75969=&index_pmcode_82127=&index_pmcode_99901=&index_pmcode_99902=&index_pmcode_99903=&index_pmcode_99904=&index_pmcode_99905=&index_pmcode_99906=&index_pmcode_99907=&index_pmcode_99968=&index_pmcode_00045=&sort_key=site_no&group_key=basin_cd&sitefile_output_format=html_table&column_name=site_no&format=html_table&html_table_sort_key=site_no&html_table_group_key=NONE&rdb_compression=file&list_of_search_criteria=multiple_site_no%2Crealtime_parameter_selection#ny05
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
ParaleptropyAuthor Commented:
please omit anything after the last </html> tag.

Sorry about that.
0
 
ParaleptropyAuthor Commented:
Thank, just what I was looking for!

One more quick question...  I'd like to learn a bit more about PHP.  I can script in Winbatch pretty good.  This seams somewhat similar in a way.  How can I apply what I know in winbatch to this?  Got any good sites to start off with?

Thanks
0
 
RoonaanCommented:
I am not familiar with winbatch at all. I know a little dos-batch, but that goes as far as 'goto'.

As to php there are many useful sites. Are you a manual kind of programmer or one that learns from examples/downloaded scripts?
0
 
ParaleptropyAuthor Commented:
I like to learn from examples that explain what's going on.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.