Advertisement
Guest User

Untitled

a guest
Sep 24th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   <script type="text/javascript">
  2.     function grabLatestRecord(){
  3.       var temp;
  4.       var humid;
  5.       var time;
  6.       temp =
  7.                   <?php
  8.                     $db = new SQLite3('local.db');
  9.                     $results = $db->query("SELECT temperature FROM readings WHERE id = (SELECT MAX(id) FROM readings)");
  10.                     while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
  11.                       ob_start();
  12.                       var_dump($row);
  13.                       $content = ob_get_contents();
  14.                       ob_end_clean();
  15.                     }
  16.                     echo json_encode($content);
  17.                   ?>
  18.                   ;
  19.  
  20.       humid =
  21.                   <?php
  22.                     $db = new SQLite3('local.db');
  23.                     $results = $db->query("SELECT humidity FROM readings WHERE id = (SELECT MAX(id) FROM readings)");
  24.                     while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
  25.                       ob_start();
  26.                       var_dump($row);
  27.                       $content = ob_get_contents();
  28.                       ob_end_clean();
  29.                     }
  30.                     echo json_encode($content);
  31.                   ?>
  32.                   ;
  33.  
  34.       time =
  35.                   <?php
  36.                     $db = new SQLite3('local.db');
  37.                     $results = $db->query("SELECT time FROM readings WHERE id = (SELECT MAX(id) FROM readings)");
  38.                     while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
  39.                         ob_start();
  40.                         var_dump($row);
  41.                         $content = ob_get_contents();
  42.                         ob_end_clean();
  43.                     }
  44.                     echo json_encode($content);
  45.                   ?>
  46.                   ;
  47.  
  48.       var results = cleanResults(temp, humid, time);
  49.  
  50.       printLatestRecords(results[0], results[1], results[2]);
  51.  
  52.     }
  53.  
  54.     function cleanResults(temp, humid, time){
  55.       var resultArray = [temp, humid, time];
  56.  
  57.       //Temperature
  58.       var firstOpen = resultArray[0].indexOf("(");
  59.       var firstClose = resultArray[0].indexOf(")");
  60.       resultArray[0] = resultArray[0].substring((resultArray[0].indexOf("(", (firstOpen + 1))) + 1, (resultArray[0].indexOf(")", (firstClose + 1))));
  61.  
  62.       //Humidity
  63.       firstOpen = resultArray[1].indexOf("(");
  64.       firstClose = resultArray[1].indexOf(")");
  65.       resultArray[1] = resultArray[1].substring((resultArray[1].indexOf("(", (firstOpen + 1))) + 1, (resultArray[1].indexOf(")", (firstClose + 1))));
  66.  
  67.       //Time
  68.       thirdOccurence = resultArray[2].indexOf('"', resultArray[2].indexOf('"', resultArray[2].indexOf('"') + 1) + 1);
  69.       fourthOccurence = resultArray[2].indexOf('"', thirdOccurence + 1);
  70.       resultArray[2] = resultArray[2].substring(thirdOccurence + 1, fourthOccurence);
  71.  
  72.       return resultArray;
  73.     }
  74.  
  75.     function printLatestRecords(temp, humid, time){
  76.       document.getElementById("temp").innerHTML = temp;
  77.       document.getElementById("humid").innerHTML = humid;
  78.       document.getElementById("time").innerHTML = time;
  79.     }
  80.  
  81.     function test(){
  82.       document.getElementById("debug").innerHTML = "DEBUG!";
  83.     }
  84.  
  85.     grabLatestRecord();
  86.  
  87.   </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement