1

I have a code which will Display the log file data in the attached format, Here it will display all the readings in the table I need to display only the Latest or Last Reading how to display that?Please help me.I have attached the Log file in the link Log File

 <meta http-equiv="refresh" content="10" > 
 <html>
 <h1><center>Online Real Time Temperature and Pressure Monitoring System</center></h1>
 <head>
 <style>
 table,th,td{
border:1px solid black;
border-collapse: collapse;
}
th,td{
padding: 5px;
}
</style>
</head>
<body>
<table align ="center" style="width:60%">
<tr>
<th>Year</th>
<th>Month</th>
<th>Date</th>
<th>Time</th>
<th>Sensor ID</th>
<th>Position</th>
<th>Pressure(psi)</th>
<th>Temperature</th>
</tr>
</body>
</html>
<?PHP
error_reporting(E_ALL & ~E_NOTICE);
$file_handle = fopen("D:/192.168.1.12_10000-gprs20160525.log", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(' ', $line_of_text);
$finalstr=$parts[0]."<BR>";
$hello=explode('>',end($parts));
$hello2=$hello[0]."<BR>";
$hello3=strlen($hello2);
if($hello3==23){
$timestamp=$hello2;
}
elseif($hello3==27)
{
$data=$hello2;
$hello4=explode('-',$timestamp."-".$data);
$year = substr($hello4[0],1,4);
$Month = substr($hello4[0],5,2);
$Date = substr($hello4[0],7,2);
$Hour = substr($hello4[0],9,2);
$Min = substr($hello4[0],11,2);
$sec =  substr($hello4[0],13,2);
$time= $Hour.":".$Min.":".$sec;
$sensorid1=substr($hello4[1],4,2);
$sensorid2=substr($hello4[1],7,2);
$Pressure1=substr($hello4[1],10,2);
$Pressure2=substr($hello4[1],13,2);
$temperature=substr($hello4[1],16,2);
$finalsensor=$sensorid1.$sensorid2;
$finalPressure=$Pressure1.$Pressure2;
$finalPressure1=(hexdec($finalPressure)-1531)/100+0.61;
$finaltemperature1=hexdec($temperature)-40;
$position="FL";

 ?>
 <html>
 <head>

 </head>
 <body>

  <tr>
  <td><center><?php echo ($year);?></center></td>
  <td><center><?php echo ($Month); ?></center></td>
  <td><center><?php echo ($Date); ?></center></td>
  <td><center><?php echo ($time); ?></center></td>
  <td><center><?php echo ($finalsensor); ?></center></td>
  <td><center><?php echo ($position); ?></center></td>
  <td><center><?php echo ($finalPressure1); ?></center></td>
  <td><center><?php echo ($finaltemperature1); ?></center></td>


  </tr>
  </body>
 </html>
  <?php
  }
  elseif($hello3==50)
  {
   echo "";
   }
   }
  fclose($file_handle);

   ?>
  • your link shows a 404, but presuming each line contains a new record, this may be what you are looking for http://stackoverflow.com/questions/1510141/read-last-line-from-file – SML Jun 22 '16 at 04:22
  • Link shows 404 error but on the top left corner you can find the download option which will download the file, – Harinath Devineni Jun 22 '16 at 04:25
  • I've seen the link you have posted ,but The data logging in the file has lot of unwanted data which i will filter in the code and i need Filtered data only – Harinath Devineni Jun 22 '16 at 04:26
  • 1
    Possible duplicate of [What is the best way in PHP to read last lines from a file?](http://stackoverflow.com/questions/15025875/what-is-the-best-way-in-php-to-read-last-lines-from-a-file) – Tigger Jun 22 '16 at 05:09

2 Answers2

0

Display only the last line in that file.

Calculate total lines in that file, Iterate each line and skip the line if it is not the last line.

josdev
  • 81
  • 1
  • 11
0

Your code re-assigns $data everytime 27 characters are detected, if you want to print just the last set of result, simply move the string decomposition (all the substr()) and echoing part outside the while loop.

There is no need to close and reopen the <html> and <body> tags between parts.

If the latest value is all you need, you should consider rewriting your code. Read the log bottom to top, as soon as a line containing 27 characters is detected, find the next line that contains 23 characters, then return $hello4 at first instance of assignment to break loop, way more efficient and memory friendly.

<meta http-equiv="refresh" content="10" > 
 <html>
 <h1><center>Online Real Time Temperature and Pressure Monitoring System</center></h1>
 <head>
 <style>
table,th,td{
border:1px solid black;
border-collapse: collapse;
}
th,td{
padding: 5px;
}
</style>
</head>
<body>
<table align ="center" style="width:60%">
<tr>
<th>Year</th>
<th>Month</th>
<th>Date</th>
<th>Time</th>
<th>Sensor ID</th>
<th>Position</th>
<th>Pressure(psi)</th>
<th>Temperature</th>
</tr>
<?php
error_reporting(E_ALL & ~E_NOTICE);
$file_handle = fopen("temp.log", "rb");
while (!feof($file_handle) ) {
    $line_of_text = fgets($file_handle);
    $parts = explode(' ', $line_of_text);
    $finalstr=$parts[0]."<BR>";
    $hello=explode('>',end($parts));
    $hello2=$hello[0]."<BR>";
    $hello3=strlen($hello2);
    if($hello3==23){
        $timestamp=$hello2;
    }
    elseif($hello3==27)
    {
        $data=$hello2;
        $hello4=explode('-',$timestamp."-".$data);
    }
    elseif ($hello3 == 50)
    {
        echo "";
    }
}
fclose($file_handle);
$year = substr($hello4[0], 1, 4);
$Month = substr($hello4[0], 5, 2);
$Date = substr($hello4[0], 7, 2);
$Hour = substr($hello4[0], 9, 2);
$Min = substr($hello4[0], 11, 2);
$sec = substr($hello4[0], 13, 2);
$time = $Hour . ":" . $Min . ":" . $sec;
$sensorid1 = substr($hello4[1], 4, 2);
$sensorid2 = substr($hello4[1], 7, 2);
$Pressure1 = substr($hello4[1], 10, 2);
$Pressure2 = substr($hello4[1], 13, 2);
$temperature = substr($hello4[1], 16, 2);
$finalsensor = $sensorid1 . $sensorid2;
$finalPressure = $Pressure1 . $Pressure2;
$finalPressure1 = (hexdec($finalPressure) - 1531) / 100 + 0.61;
$finaltemperature1 = hexdec($temperature) - 40;
$position = "FL";
?>
  <tr>
  <td><center><?php
echo ($year); ?></center></td>
  <td><center><?php
echo ($Month); ?></center></td>
  <td><center><?php
echo ($Date); ?></center></td>
  <td><center><?php
echo ($time); ?></center></td>
  <td><center><?php
echo ($finalsensor); ?></center></td>
  <td><center><?php
echo ($position); ?></center></td>
  <td><center><?php
echo ($finalPressure1); ?></center></td>
  <td><center><?php
echo ($finaltemperature1); ?></center></td>
  </tr>
  </body>
 </html>
SML
  • 1,235
  • 6
  • 17
  • Glad to be of help but please learn to use trim() and strpos(), you are creating unnecessary processes and arrays for nothing. You don't need to use a single explode() for this. – SML Jun 22 '16 at 09:15