How to add data record to array for each FTP to server?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jellis00
Forum Newbie
Posts: 13
Joined: Mon Nov 28, 2011 2:52 pm

How to add data record to array for each FTP to server?

Post by jellis00 »

I am experienced with HTML but I am relatively new to PHP.

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>

User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to add data record to array for each FTP to server?

Post by McInfo »

Variables are destroyed at the end of the script. They don't survive from one script execution to the next. To give your application permanent memory, you need to store data in sessions, files, or databases.

Who or what is responsible for uploading the two files via FTP? It might be better to write a script to accept a POST request and append the time and temperature to a text file or database table.
jellis00
Forum Newbie
Posts: 13
Joined: Mon Nov 28, 2011 2:52 pm

Re: How to add data record to array for each FTP to server?

Post by jellis00 »

Thanks for reply, McInfo. I now understand that I need to get the data to a non-volatile state to survive after the user leaves the site. I have started studying MySQL so I can store the FTP'd data in a database during each FTP session. Source of the FTP is an embedded microcontroller application that is using a ConnectOne iWiFi module as a bridge to a wireless LAN for internet access, so it is way to complicated to have it do POST requests. I think that I will leave the embedded microcontroller application alone since it is working well as currently programmed. I will therefore focus on using PHP to read the text files for the FTP's data elements and then INSERT them into the MySQL database for persistence.
Thanks again for your advice...it got me headed in the right direction.
Post Reply