How to modify this code to display a different table format?
Posted: Mon Jan 16, 2012 12:50 pm
I currently have PHP code that creates the table shown at this URL: [url]http://www.lodestarassoc.com/TempAlarm/TempAlarm.php
[/url
]Every hour it receives a temperature measurement over the internet that is uploaded from a remote sensor and placed in the row of a MySQL database table that is associated with the sensor source. I want to put the data into a displayed table format that is different and has two axes:
Hours across the horizontal axis and temperature on the vertical axis, but the vertical temperature axis is the range of temperatures with cells for each temperature. \
Depending on the hour and temperature measurement of the data in the MySQL table I want the measurement for that hour placed in the vertical cell for that temperature. Each hour the table fill will grow to the right according to the time with the temperature appearing vertically in the correct cell for that temperature value.
Can anyone give me any tips on how to modify my current PHP code to do this? Here is my current code:
[/url
]Every hour it receives a temperature measurement over the internet that is uploaded from a remote sensor and placed in the row of a MySQL database table that is associated with the sensor source. I want to put the data into a displayed table format that is different and has two axes:
Hours across the horizontal axis and temperature on the vertical axis, but the vertical temperature axis is the range of temperatures with cells for each temperature. \
Depending on the hour and temperature measurement of the data in the MySQL table I want the measurement for that hour placed in the vertical cell for that temperature. Each hour the table fill will grow to the right according to the time with the temperature appearing vertically in the correct cell for that temperature value.
Can anyone give me any tips on how to modify my current PHP code to do this? Here is my current code:
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="5" />
<title>TempAlarm.php</title></head>
<body>
<?php
//Read the loginName of the current User from file into variable
//include('/loginName.txt'); //for TEST only...comment out for ops
$myFile = "loginName.txt";
$fh = fopen($myFile, 'r');
$loginName = fread($fh, filesize($myFile));
fclose($fh);
echo "<h1>This is the TempAlarm site for USER= $loginName.</h1>" ;
//Read the TempAlarm serial number from file for login into variable
//include('/serial_no.txt'); //for TEST only...comment out for ops
$myFile = "serial_no.txt";
$fh = fopen($myFile, 'r');
$theSerial = fread($fh, filesize($myFile));
fclose($fh);
//Read the TempAlarm reported hourly time stamp from file into variable
//include('/hour.txt'); //for TEST only...comment out for ops
$myFile = "hour.txt";
$fh = fopen($myFile, 'r');
$theHour = fread($fh, filesize($myFile));
fclose($fh);
//Build file name for the hour of the temperature measurement
$theFile = $theHour . "hour.txt";
//echo $theFile; //for TEST only...comment out for ops
//Read the TempAlarm reported hourly temperature from file into variable
$fh = fopen($theFile, 'r');
$theTemp = fread($fh, filesize($theFile));
fclose($fh);
echo "<br />";
echo "The most recent temperature measurement at the location of TempAlarm No. " . $theSerial . " is " . $theTemp . " degrees F at time " . $theHour . ":00.";
echo "<br />";
?>
<?php
//Store latest temperature reading in appropriate time slot in mysql DB table "temphistory"
echo "<br />";
echo "The hour for this posting to the data base is " . $theHour . ".";
echo "<br />";
$theField = $theHour . "hour";
//echo $theField ; //for TEST only...comment out for ops
$host = "ichipcustomer.db.2694804.hostedresource.com";
$user = "ichipcustomer";
$passwd = "Kevin111";
$dbname = "ichipcustomer";
$cxn = mysqli_connect($host,$user,$passwd,$dbname) or die(mysql_error());
mysqli_select_db($cxn,$dbname) or die(mysqli_errno($cxn));
echo "<br />Connected to mysql database.<br />";
// Point to the record for the logged in Serial_no and login
$result = mysqli_query($cxn,"SELECT * FROM temperaturehistory
WHERE loginName = '$loginName' ") or die(mysqli_errno($cxn));
// Update the temperature in the database field of this record for this hour
$query = "UPDATE temperaturehistory SET $theField = '$theTemp'
WHERE loginName = '$loginName'";
$result = mysqli_query($cxn,$query)
or die("Couldn't execute query: "
.mysqli_error($cxn));
echo "<br />";
Print "Your temperature history database for TempAlarm # " . $theSerial . " has been populated with a new temperature for this hour.";
echo "<br />";
echo "<br />";
?>
<?php
Print "The temperature history for the last 24 hours at the location of TempAlarm # " . $theSerial . " is shown in the below table.";
$query = "SELECT * FROM temperaturehistory WHERE `serial_no` = loginName = '$loginName'" ;
$temps = mysqli_query($cxn,$query) or die ("Couldn't execute query.");
$times = array("NA","NA","00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00") ;
$row = mysqli_fetch_row($temps);
//output the Time and Temperature arrays in HTML to a table
echo '<table border="1">';
echo '<tr><td>Time</td><td>Temperature</td></tr>';
for($i=2;$i<=25;$i++)
{
echo "<tr>\n
<td>$times[$i]</td>\n
<td style='text-align: right'>$row[$i]</td>\n
</tr>\n";
}
echo "</table>\n";
?>
</body>
</html>