如何使用PHP循环获取mysql数据到谷歌图表? [英] How to get mysql data into a google chart using php loop?

查看:79
本文介绍了如何使用PHP循环获取mysql数据到谷歌图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Google图表,并添加了数据库本身的数据。当通过php循环获取数据时,我遇到了一些困难,因为由于语法的原因,我无法将这些数据修改为图表。

I've created a Google chart and I've added data taken from the database itself. When getting data through php loops though, I have had some difficulty because I couldn't amend that data to the chart because of its syntax.

这就我而言已经走了。我需要获取mysql值来代替:

1170,460,45,123,456],
660,1120,145,785,658]

This is as far as I have gone. I need to get the mysql values in place of:

1170, 460, 45, 123, 456], 660, 1120, 145, 785, 658]

    var data = google.visualization.arrayToDataTable([
      ['Term', <?php

      while ($getchartrows =  mysql_fetch_array($getchart))
      {
          echo  " ' " . $getchartrows['std_ID'] . " ' " . ",";
      }

      ?>],

      <?php


      $one = mysql_query("SELECT * FROM stusitting WHERE std_ID = '0001' AND subjectNo = '$subin' AND grade = '$gradein' AND termNo = '$tcheck' ");
      $getone = mysql_fetch_array($one);

      ?>

          ['01',  <?php echo $getone ['marks']; ?>,      400, 495, 565, 415],
      ['02',  1170,      460, 45, 123, 456],
      ['03',  660,       1120, 145, 785, 658]
    ]);




    var options = {
      title: 'Class Progress'
    };


推荐答案

试着解决另一个问题。首先,从数据库中获取所需的数据。然后,在PHP中创建您需要的数据结构,并使用 json_encode()将其转换为JavaScript。最后,将它传递给JavaScript中的可视化功能:

Try to tackle one problem after the other. First, get the data you need from the database. Then, create the data structure you need in PHP, and convert it to JavaScript using json_encode(). Finally, pass it to the visualization function in JavaScript:

<?php
// query data
$result = mysql_query(...);

// format data structure
$data = array();
$i = 0;
while($row = mysql_fetch_array($result)) {
    $i += 1;
    array_push($data, array($i) + $row);
}

// convert to JavaScript
?>
var raw_data = <?php echo json_encode($data) ?>;

// visualize
var data = google.visualization.arrayToDataTable(raw_data);

这篇关于如何使用PHP循环获取mysql数据到谷歌图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆