dongpian2559 2015-02-12 14:59
浏览 70
已采纳

如何在不刷新页面的情况下不断更新PHP变量?

I'm making a website that shows data from a MySQL database in Morris graphs. Basically I have a database that gets new measurements every minute, and I'm trying to show these changes live without having to reload the whole page. I have shortened my code for this question, but here's basically what I have:

PHP code:

<?php
   while($measurement = mysqli_fetch_array($result)){
       $data += $measurement['data'];
   }
?>

And the script:

function data() {
        var ret = [];
        ret.push({
          y: 'Today',
          a: <?php echo $data; ?>
        });
      return ret;
    }

var graph = Morris.Bar({
        element: 'graph',
        data: data(),
        xkey: 'y',
        ykeys: ['a'],
        labels: ['random label']
    });

function update() {
      graph.setData(data());
    }

setInterval(update, 60000);

The graph is then shown in a div with the id "graph". So the problem is that the update function doesn't update the graph with new data as $data doesn't get updated. I've heard that I can somehow create a PHP function and continuously run that using Ajax and have it update my $data variable, but I have no idea how to do that.

In order for the graph to update I have to reload the whole page, and this works fine using a meta tag that resfreshes the page every 60 seconds, but it seems like a bad solution.

I have also tried to put the code in a separate PHP file and run that using this code:

var auto_updater = setInterval(
    (function () {
        $("#graph").load("data.php");
    }), 60000);

This also works fine, but the problem is that it redraws the whole graph and that causes the scroll bar on my site to go crazy. What I want is to update the data variable in Morris.Bar, not the whole thing. Any help would be appreciated.

  • 写回答

3条回答 默认 最新

  • doutongwei4380 2015-02-12 15:06
    关注

    Edit: because you only ever need one value, your json will only be that value. Though technically that does not make for valid json (there should be an object at the top level) it works with jquery just fine.

    PHP: (data.php)

    <?php
      $data = // obtain current value of data from somewhere
      echo $data; // should be an integer
    ?>
    

    JS:

    $(document).ready( function () {
      var data = []; // data is empty
      var graph = Morris.Bar({
        element: 'graph',
        data: data
        labels: ['random label']
      });
    
      function update () {
        $.getJSON( "data.php", function (newv) {
          data.push( { x: newv, y: "Today" } ); // store new value
          graph.setData( data );                // update the graph
        });
      }
    
      update();
      setInterval( update, 1000 );
    });
    

    and that's everything to it!


    It is impossible to "keep PHP running in the background" or something like that, your attempt is correct. However instead of loading new HTML you should load the pure data (for example as a JSON document) and then push that data into your existing dataset via JS. Using JS to grab updated data from the server is accomplished with AJAX.

    You could for example use $.getJSON

    Edit: The full JS would look like this:

    var initial_data = <?php echo $data; ?> // or leave this out and replace with another $.getJSON for clean code!
    $(document).ready( function () {
      var graph = Morris.Bar( ... );
    
      setInterval( function () {
        $.getJSON( "data.php", function (data) {
          graph.setData( data );
        });
      }, 1000 );
    });
    

    and the response from PHP should look something like this:

    [
      { "x": 10, "y": 20 },
      { "x": 3, "y": 12 },
      { "x": 8, "y": 19 }
    ]
    

    so the PHP would look like this maybe:

    [
      <?php
        // first one seperately because it can't have a leading comma - JSON is strict
        $measurement = mysqli_fetch_array($result)
        echo '{ "x": ' . $result["data"]["x"] . ', "y": ' . $result["data"]["x"] . ' }';
        while($measurement = mysqli_fetch_array($result)) {
         // no idea what data looks like, I'm assuming it has x and y properties
         echo ', { "x": ' . $result["data"]["x"] . ', "y": ' . $result["data"]["x"] . ' }';
        }
      ?>
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?