Page 1 of 1

How to modify this code to display a different table format?

Posted: Mon Jan 16, 2012 12:50 pm
by jellis00
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:

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>


Re: How to modify this code to display a different table for

Posted: Mon Jan 16, 2012 1:59 pm
by twinedev
So are you wanting this to be like a graph over the times (shaded columns to the correct temp "height" in the table) or that it will show that hours temp in the appropriate height?

-Greg

Re: How to modify this code to display a different table for

Posted: Mon Jan 16, 2012 11:30 pm
by jellis00
No, Greg, I really want it to appear more like a spreadsheet table with the dates and corresponding AM / PM measurement times horizontal and the temperatures vertical. Something like this (shown only for 15 days):
------------------------------------------------------------------------------------------------
Date | 1 | 2 | 3 | 4 | 5 | Etc, etc, | 13 | 14 | 15 |
------------------------------------------------------------------------------------------------
|AM|PM|AM|PM|AM|PM| AM|PM|AM| PM| |AM| PM|AM|PM|AM| PM|
------------------------------------------------------------------------------------------------------------
11 deg | | | | | | | | | | | | | | | | | |
------------------------------------------------------------------------------------------------------------
10 deg | | | | | | | | | | | | | | | | | |
------------------------------------------------------------------------------------------------------------
9 deg | | | | | | | | | | | | | | | | | |
------------------------------------------------------------------------------------------------------------
8 deg | | | | | | | | | | | | | | | | | |
------------------------------------------------------------------------------------------------------------
7 deg | | | | | | | | | | | | | | | | | |
------------------------------------------------------------------------------------------------------------
etc, etc.

For each date I want the code to put an X in the cell that equates to the temperature at the AM time and also at the PM time. This is the client's required format. Un fortunately the forum won't let me format it that way with spaces between the vertical brackets, but hopefully this gives you the idea.

Re: How to modify this code to display a different table for

Posted: Tue Jan 17, 2012 1:44 am
by Christopher
Probably the easiest way would be to get the data in an array first by degrees with each degree having an array of dates containing temperature values:

Code: Select all

$data = array(
    11 => array(
        1 => 34,
        2 => 33,
...
        15 => 31,
        ),
    10 => array(
        1 => 32,
        2 => 31,
...
        15 => 30,
        ),
...
);
Then just loop through it -- the outer loop is rows and the inner columns.