0

I am trying to create a Real-time chart, with the data pulling from my own server. I am using the setInterval function to refresh the data from the server, but I can't refresh the value. Can anyone help me with this? Thank you Here is my source code:

main.php


<?php 
require 'data_refresh.php';
?>

<script type="text/javascript">

function pull(){

   value1 = <?php echo $value1; ?>;
   reading_time = <?php echo $reading_time; ?>;
},
pull();

var chartT = new Highcharts.Chart({

 //Some code for creating the chart

});

setInterval(pull(),2000);

Here is the data_refresh.php file

<?php

$servername = "";

$dbname = "";

$username = "";

$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, value1,reading_time FROM sensordata order by reading_time desc limit 40";

$result = $conn->query($sql);

while ($data = $result->fetch_assoc()){
    $sensordata[] = $data;
}

$readings_time = array_column($sensordata, 'reading_time');



$value1 = json_encode(array_reverse(array_column($sensordata, 'value1')), JSON_NUMERIC_CHECK);
$reading_time = json_encode(array_reverse($readings_time), JSON_NUMERIC_CHECK);




$result->free();
$conn->close();
?>


  • Is setInterval better solution for real time app, really ? and why are you calling pull() just after the function body pull() ? when you already calling using setInterval – Deep Kakkar Mar 05 '20 at 09:37
  • 1
    php only render server side.setinterval execute on browser side. That `setInterval` not refresh the php data .so better use ajax call https://stackoverflow.com/questions/4930439/call-jquery-ajax-request-each-x-minutes – prasanth Mar 05 '20 at 09:39
  • I think you need to read this: [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – ADyson Mar 05 '20 at 09:41

0 Answers0