我有一个代码,它将以附加格式显示日志文件数据, 在这里它将显示表格中的所有读数 我只需要显示最新或最后阅读如何显示?请帮帮我。我已经在链接中附加了日志文件 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);
?>
答案 0 :(得分:0)
仅显示该文件中的最后一行。
计算该文件中的总行数, 如果它不是最后一行,则迭代每一行并跳过该行。
答案 1 :(得分:0)
每次检测到27个字符时,您的代码会重新分配$data
,如果您只想打印最后一组结果,只需移动字符串分解(所有substr()
)并回显部分外部while
循环。
无需关闭并重新打开各部分之间的<html>
和<body>
标记。
如果您需要的是最新值,则应考虑重写代码。从底部到顶部读取日志,一旦检测到包含27个字符的行,找到包含23个字符的下一行,然后在第一个赋值实例中返回$hello4
以打破循环,方式更有效且内存友好。
<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>