DB重新查询用于envoriment监控的php

时间:2017-06-27 22:37:30

标签: php html phpmyadmin

我是新来的,这是我的第一篇文章 我需要大学项目的帮助。 我们正在编写树莓派,以便测量温度和空气湿度,并在html页面上显示数据。 但我们有一个问题是使可视化正确。 我们从myadminphp获取数据并以图形方式显示,但时间戳不稳定,它会在当下减去3小时的时间,但我们希望每3小时修复一次。

Php代码:

$lastDatetime = $db->query("select date_time 
                            from measurements 
                            where id=(
                                        select max(id) 
                                        from measurements
                                    )")
                    ->fetch(PDO::FETCH_ASSOC)["date_time"];
$thisDay = $lastDatetime;
$dataLastSevenDays = array();
for ($i = 0; $i < 56; $i++) {
    $maxTemperatureThisDay = $db->query("select value 
                                         from measurements 
                                         where date_time like '" . substr($thisDay, 0, 13) . "%' 
                                           and channel = 1 
                                         limit 1"
                                         )
                            ->fetch(PDO::FETCH_ASSOC)["value"]; 
    //channel 1 Temp
    $maxBrightnessThisDayThisDay = 
        $db->query("select value 
                    from measurements 
                    where date_time like '" . substr($thisDay, 0, 13) . "%' 
                      and channel = 2 
                    limit 1")
            ->fetch(PDO::FETCH_ASSOC)["value"]; //channel 2 Licht

    $maxHumidityThisDay = 
        $db->query("select value 
                    from measurements 
                    where date_time like '" . substr($thisDay, 0, 13) . "%' 
                      and channel = 4 
                    limit 1")
            ->fetch(PDO::FETCH_ASSOC)["value"]; //channel 4 Luftfeuchtigkeit
    $thisDay = date_sub(new DateTime($thisDay), new DateInterval('PT10800S'))->format('Y-m-d H:i:s');
    $dataLastSevenDays[$i] = array($thisDay, $maxTemperatureThisDay, $maxBrightnessThisDayThisDay, $maxHumidityThisDay); 
}

html代码:

<script>
var ctx = document.getElementById('temperature').getContext('2d');
var chart = new Chart(ctx, {
                            type: 'line',
                            data: {
                                    labels: [
<?php
    for ($i = 56; $i > 0; $i--) {
        echo '"' . $dataLastSevenDays[$i][0] . '",';
    }
?>
                                    ],
                             datasets: [{
                                        label: "gemessene Temperatur",
                                        borderColor: 'rgb(16, 119, 237)',
                                        data: [
<?php
    for ($i = 56; $i > 0; $i--) {
        echo '"' . $dataLastSevenDays[$i][1] . '",';
    }
?>
                                            ]}]
                                },
                                options: {}
                            });
</script>

0 个答案:

没有答案