I currently have part of the below code working which reads two data values that were FTP'd to particular files on the server into two variables, $theTime and $theTemp. This data is updated by FTP every hour to the server via two file reads into the two variables. The current but incorrect results from the below code can be seen at my website:
http://www.lodestarassoc.com/iChip/iChip.php
As you can see the FTP's data is correctly concatenated into a text string by the first part of the PHP script. However, I want the server side PHP script to create a new record (a new row in the 2-dimensional array), and then update a table with the new record being added as a new row. My below code does create a new record to the single record array by use of the array_push function. This is confirmed by my print of the main array that shows two records, the initial empty record and the data contained in the first FTP of a new record.
However, everytime the hourly FTP update occurs it doesn't add a new record to the array...it just overwrites the 2nd record that was previously created in the array by the previous FTP. I want each FTP to create and add a new record containing the new FTP'd data...not the previously FTP'd data. In effect, I am trying to create a data table that generates a new row in the data table every hour.
What I want for a resulting table is rows of records with two fields in the record...one for the time data contained in file include-4.txt that is read into variable $theTime and the other from the temperature data uploaded into file include-2.txt and read into variable $theTemp. I presumed the function 'array_push' would add a new record onto the table every hour until 23 records have been created, and then I need to figure out how to start over at time 00:00. I thought array_push function would do this for me by adding each FTP'd data set to the end of the array, and then by monitoring the number of records generated with count I could then start over at the first row of the table when time 23:00 is reached. I can't figure out how to do this.
Can anyone help me. This is very frustrating to me as a new PHP user.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html" charset="utf-8" http-equiv="refresh" content="150" />
<title>iChip.php</title></head>
<body>
<?php
//include('/include-2.txt');
$myFile = "include-2.txt";
$fh = fopen($myFile, 'r');
$theTemp = fread($fh, filesize($myFile));
fclose($fh);
$myFile = "include-4.txt";
$fh = fopen($myFile, 'r');
$theTime = fread($fh, filesize($myFile));
fclose($fh);
echo "<br />";
echo "The most recent inside temperature measurement at 206 N. Veterans is " . $theTemp . " degrees F at time " . $theTime . ".";
//Initialize the record arrays
//Initialize the record arrays
$main = array();
$row_0 = array(0,0); //creates and empty row as first row of array
// now push the first row array onto the main array
array_push ($main, $row_0);
// okay so lets input latest data into a new row and add new row to main array
$new_row = array($theTime, $theTemp);
array_push ($main, $new_row);
// lets print out the $main array
echo "<h1>Here is a print out of the multidimensional array elements</h1>";
echo "<h1>that shows the two data elements that were FTP'd to the files</h1>";
echo "<h1>on the server were added to the array by the array_push function.</h1>";
echo "<h1>Each FTP just writes over the previous record in the array rather than creating a new one.h1>";
echo "<pre>";
print_r($main);
echo "</pre>";
?>
<?php
echo "<h1>Here is one way to display the multidimensional array in a table</h1>";
echo "<h1>but it doesn't show the Time and Temperature as a row</h1>";
include("show_array.php"); //displays a called multidimensional array
html_show_array($main);
?>
<?php
echo "<h1>Here is another way to attempt to display the multidimensional array in a table</h1>";
echo "<h1>but it doesn't show the Time and Temperature as a row</h1>";
//let's print out the headers to our table
echo "<table border='1' cellpadding='5'>";
echo"<tr><th>Time</th><th>Temperature</th></tr>";
//Now we start the foreach loop using the variable $Time to hold our key
//and $Degrees to hold our temperature measurement.
foreach($main as $Time=>$Degrees)
{
echo "<tr><td>$Time </td><td>$Degrees</td></tr> "; //print the values into a table cell for each iteration
}
//finally close the table
echo "</table>";
?>
</body>
</html>