Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- function grabLatestRecord(){
- var temp;
- var humid;
- var time;
- temp =
- <?php
- $db = new SQLite3('local.db');
- $results = $db->query("SELECT temperature FROM readings WHERE id = (SELECT MAX(id) FROM readings)");
- while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
- ob_start();
- var_dump($row);
- $content = ob_get_contents();
- ob_end_clean();
- }
- echo json_encode($content);
- ?>
- ;
- humid =
- <?php
- $db = new SQLite3('local.db');
- $results = $db->query("SELECT humidity FROM readings WHERE id = (SELECT MAX(id) FROM readings)");
- while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
- ob_start();
- var_dump($row);
- $content = ob_get_contents();
- ob_end_clean();
- }
- echo json_encode($content);
- ?>
- ;
- time =
- <?php
- $db = new SQLite3('local.db');
- $results = $db->query("SELECT time FROM readings WHERE id = (SELECT MAX(id) FROM readings)");
- while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
- ob_start();
- var_dump($row);
- $content = ob_get_contents();
- ob_end_clean();
- }
- echo json_encode($content);
- ?>
- ;
- var results = cleanResults(temp, humid, time);
- printLatestRecords(results[0], results[1], results[2]);
- }
- function cleanResults(temp, humid, time){
- var resultArray = [temp, humid, time];
- //Temperature
- var firstOpen = resultArray[0].indexOf("(");
- var firstClose = resultArray[0].indexOf(")");
- resultArray[0] = resultArray[0].substring((resultArray[0].indexOf("(", (firstOpen + 1))) + 1, (resultArray[0].indexOf(")", (firstClose + 1))));
- //Humidity
- firstOpen = resultArray[1].indexOf("(");
- firstClose = resultArray[1].indexOf(")");
- resultArray[1] = resultArray[1].substring((resultArray[1].indexOf("(", (firstOpen + 1))) + 1, (resultArray[1].indexOf(")", (firstClose + 1))));
- //Time
- thirdOccurence = resultArray[2].indexOf('"', resultArray[2].indexOf('"', resultArray[2].indexOf('"') + 1) + 1);
- fourthOccurence = resultArray[2].indexOf('"', thirdOccurence + 1);
- resultArray[2] = resultArray[2].substring(thirdOccurence + 1, fourthOccurence);
- return resultArray;
- }
- function printLatestRecords(temp, humid, time){
- document.getElementById("temp").innerHTML = temp;
- document.getElementById("humid").innerHTML = humid;
- document.getElementById("time").innerHTML = time;
- }
- function test(){
- document.getElementById("debug").innerHTML = "DEBUG!";
- }
- grabLatestRecord();
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement