I have a PHP script that returns the result in CSV format:
<?php
include '../misc.php';
$values=$_GET?$_GET:$_POST;
{
$ts = isset($values['t'])?strtotime($values['t']):strtotime("-1 hour");
$result=Query('iot.data',['key' => $values['key'], 'timestamp' => array('$gte' => $ts)],['projection' => array('_id' => 0, 'timestamp' => 1, $values['col'] => 1), 'sort' => array('timestamp' => 1)]);
if ($values['col'] == 'temp1') echo "Timestamp, Temperature\n";
if ($values['col'] == 'BusV2') echo "Timestamp, Battery V\n";
if ($values['col'] == 'uv1') echo "Timestamp, UV\n";
foreach ($result as $r) {
$d = date('c',$r->timestamp);
if (isset($r->temp1)) echo "$d, $r->temp1\n";
if (isset($r->BusV2)) echo "$d, $r->BusV2\n";
if (isset($r->uv1)) echo "$d, $r->uv1\n";
};
};
With the col parameter, I can decide which key will be returned with a timestamp.
How can I get the desired result without these IFs inside the foreach loop?
How do I encode something like:
foreach ($result as $r) {
$d = date('c',$r->timestamp);
echo "$d, $r->$values['col']\n" // where object $r key comes from variable 'col'
}
This will not help if I changed this foreach to:
foreach ($result as $r => $v) {
because it $vdoes not receive the key value $r, $vnow what $rcame before ...
Result print_r($v);
stdClass Object
(
[temp1] => 25.2
[timestamp] => 1470304854
)
source
share