Skip to content

Instantly share code, notes, and snippets.

@ipimpat
Last active December 29, 2015 03:09
Show Gist options
  • Save ipimpat/55043aeede6b9797e3d2 to your computer and use it in GitHub Desktop.
Save ipimpat/55043aeede6b9797e3d2 to your computer and use it in GitHub Desktop.
<?php
// Include database connection
include 'connect.php';
// Declare result array
$result = array(
'nameA' => ' Temperatur',
'nameB' => ' Setpoint',
'category' => array(),
'data1' => array(),
'data2' => array(),
);
// Instantiate a new "to" DateTime
$to = new DateTime();
// Get MySQL connection resource
$mysql = GetMyConnection();
// Check if "dateParamA" request query parameter exists and is not empty
// and alter "to" DateTime object timestamp to the value of "dateParamA" request query parameter
if(array_key_exists('dateParamA', $_GET) AND !empty($_GET['dateParamA']) AND !$to->modify($_GET['dateParamA']))
{
throw new RuntimeException("Failed to interpret dateParamA query parameter");
}
// Clone to DateTime object, so we get a new "from" DateTime with same timestamp
$from = clone $to;
// Move "from" timestamp one week back
$from->modify('-1 week');
// Delcare SQL query string
$sql = "SELECT `Time`,`SP1`, `Tilgang` FROM `ebpool` WHERE (DATE(`Time`) BETWEEN '" . $from->format('Y-m-d') . "' AND '" . $to->format('Y-m-d') . "')";
// Execute select query
if(!$query = mysql_query($sql, $mysql))
{
throw new RuntimeException("Query error: " . mysql_errno($mysql) . ": " . mysql_error($mysql));
}
// Fetch each result row
while($row = mysql_fetch_array($query))
{
$result['category'][] = $row['Time'];
$result['data1'][] = $row['Tilgang'];
$result['data2'][] = $row['SP1'];
}
// Clean up database result
CleanUpDB();
/*
echo "<pre>";
var_dump ($_GET);
var_dump($from);
var_dump($to);
var_dump($sql);
var_dump($result);
echo "</pre>";
*/
//Output result as JSON ;
echo json_encode($result, JSON_NUMERIC_CHECK);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment