首先,谢谢您阅读我的文章。 我有这样的代码。
<?php
$con=mysqli_connect("localhost","u855423165_ardui","31039731","u855423165_ardui");
$sql="SELECT * FROM tempLog ORDER BY timeStamp ASC";
$result=mysqli_query($con,$sql);
while ($data = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$data_1[] = $data;
}
mysqli_close($con);
?>
我想进行$data_1[] = $data;
每秒更新,然后从更新数据库中进行更新,或者可以说$sql="SELECT * FROM tempLog ORDER BY timeStamp ASC";
重新运行以获取最新数据。
和我的第二个代码(图表代码)
<script type="text/javascript">
AmCharts.makeChart("chartdiv",
{
"type": "serial",
"categoryField": "timeStamp",
"dataDateFormat": "YYYY-MM-DD HH:NN:SS",
"maxSelectedSeries": -1,
"zoomOutButtonTabIndex": 0,
"sequencedAnimation": false,
"backgroundColor": "#FFFFFF",
"fontFamily": "Cabin",
"theme": "dark",
"export": {
"enabled": true
},
"categoryAxis": {
"minPeriod": "ss",
"parseDates": true
},
"chartCursor": {
"enabled": true,
"categoryBalloonDateFormat": "JJ:NN:SS"
},
"chartScrollbar": {
"enabled": true,
"dragIconWidth": 37,
"graphType": "line",
"gridColor": "#000000",
"hideResizeGrips": true,
"minimum": 1,
"offset": 2,
"tabIndex": 3
},
"trendLines": [],
"graphs": [
{
"bullet": "round",
"id": "AmGraph-1",
"title": "graph 1",
"valueField": "timeStamp"
},
{
"bullet": "round",
"id": "AmGraph-2",
"lineThickness": 3,
"title": "graph 2",
"valueField": "sensor1"
},
{
"bullet": "round",
"id": "AmGraph-3",
"lineThickness": 3,
"title": "graph 3",
"valueField": "sensor2"
},
{
"bullet": "round",
"id": "AmGraph-4",
"lineThickness": 3,
"title": "graph 4",
"valueField": "sensor3"
},
{
"bullet": "round",
"id": "AmGraph-5",
"lineThickness": 3,
"title": "graph 5",
"valueField": "sensor4"
},
{
"bullet": "round",
"id": "AmGraph-6",
"lineThickness": 3,
"title": "graph 6",
"valueField": "sensor5"
},
{
"bullet": "round",
"id": "AmGraph-7",
"lineThickness": 3,
"title": "graph 7",
"valueField": "sensor6"
}
],
"guides": [],
"valueAxes": [
{
"id": "ValueAxis-1",
"title": ""
}
],
"allLabels": [],
"balloon": {},
"titles": [
{
"id": "Title-1",
"size": 15,
"text": "Grafik 6 Sensor"
}
],
"dataProvider": <?php echo json_encode($data_1); ?>
}
);
</script>
我想将新变量插入第二个代码并更新图表。
这就是我如何插入变量"dataProvider": <?php echo json_encode($data_1);
答案 0 :(得分:1)
最简单的方法是将每秒循环一次移动到JavaScript,并使其每秒调用一次PHP,因为这在PHP / JS应用程序中更为常见。
PHP可以使用ReactPHP或Swoole + Web套接字之类的库执行实时应用程序,但是您应该意识到异步编程是一个高级概念,带来了一系列复杂性
作为初学者,您可能会发现NodeJS + socket.io比PHP更习惯地处理此问题,但您可能还需要将数据库更改为反应性数据库或设置事件/发布子系统。
我要说的是,这是一个很深的兔子漏洞,建议您在JavaScript中运行检查循环以保持简单