0

由于SO(特别是这个)和一般的内部网络上有很多不同的资源,我已经设法用mySQL创建了一个数据透视表。

你可以在这里看到一个工作示例(有限数据):SQL Fiddle

现在我想通过 PHP 获取这些数据并将其转换为 json 字符串,以便我可以将其作为 ajax 响应发送到我试图通过谷歌可视化显示图表(柱形图)的 HTML 页面,就像这样一:https ://code.google.com/apis/ajax/playground/?type=visualization#column_chart

我的问题是:如何将数据透视表转换为正确的格式以供图表理解?

我的 php 文件 (sph.php) 的 echo(ed) 结果最终得到如下结构的数据:

[{"ps_job_serial_num":"888888","105":null,"104":"4.00","400":null,"101":"5.00","102":"3.00","204":"6.00","103":"2.00","399":"1.00","300":"1.00","205":"7.00","203":"2.00","404":null,"405":null,"202":null,"301":null,"106":null,"304":null,"401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"999999","105":null,"104":"2.00","400":null,"101":"1.00","102":"0.00","204":null,"103":null,"399":"3.00","300":"2.00","205":"3.00","203":null,"404":null,"405":null,"202":null,"301":null,"106":null,"304":null,"401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"111111","105":"1.00","104":"3.00","400":null,"101":"5.00","102":"4.00","204":"10.00","103":"7.00","399":"1.00","300":"2.00","205":null,"203":null,"404":null,"405":null,"202":null,"301":null,"106":null,"304":"1.00","401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"222222","105":null,"104":"1.00","400":null,"101":"1.00","102":"1.00","204":"3.00","103":"1.00","399":null,"300":null,"205":null,"203":null,"404":"3.00","405":null,"202":null,"301":null,"106":null,"304":null,"401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"333333","105":"2.00","104":"8.00","400":null,"101":"8.00","102":"9.00","204":"10.00","103":"8.00","399":"2.00","300":"5.00","205":"8.00","203":"8.00","404":null,"405":"7.00","202":"8.00","301":null,"106":"1.00","304":null,"401":null,"201":"6.00","402":null,"403":"6.00","303":null},
{"ps_job_serial_num":"444444","105":"2.00","104":"5.00","400":null,"101":"8.00","102":"9.00","204":"10.00","103":"8.00","399":"4.00","300":"3.00","205":"8.00","203":"5.50","404":"2.00","405":"8.00","202":"8.00","301":"2.00","106":"4.00","304":null,"401":"10.00","201":"10.00","402":"7.00","403":"7.00","303":"2.00"},

etc

但我真的希望数据格式如下:

([
['ps_job_serial_num', '101', '102', '103', '104', '105', '106'],
['888888',  500,   381,   381,   110,   665,  157],
['999999',  750,   396,   381,   115,   594,  173],
['111111',  120,   406,   381,   115,   571,  167],
['222222',  100,   460,   381,   116,   619,  185],
['333333',  430,   401,   381,   120,   642,  195],
['444444',  120,   679,   381,   128,   624,  198]
]);

如何使我的数据看起来像它需要的样子?

这是我的完整 PHP 代码:

<?php
    $mysqli = new mysqli("localhost", "user", "password", "database");
    $query = "CALL new_procedure()";
    $result = $mysqli->query($query);

    while ($row = $result->fetch_assoc()) {$results_array[] = $row;}
    $jsontable = json_encode($results_array);
    echo $jsontable;
?>

这是完整的 HTML 页面:

<head>
    <title>My realtime chart</title>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
                    // Load the Visualization API and the piechart package.
        google.load('visualization', '1', {'packages':['corechart']});

        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChart);

        function drawChart() {
          var jsonData = $.ajax({
              url: "sph.php",
              dataType:"json",
              async: false
              }).responseText;

          // Create our data table out of JSON data loaded from server.
          var data = new google.visualization.DataTable(jsonData);

          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
          chart.draw(data, {width: 1200, height: 600});
        }
        </script>  
</head>

<body>
  <div id="chart_div" style="width: 1200px; height: 600px;"></div>
</body>

</html>

HTML 页面上显示的错误是“表没有列”...

更新#1

在进行@asgallant 建议的更改(如下)之后,从 PHP 返回的数据现在如下所示:

[
["ps_job_serial_num",105,104,400,101,102,204,103,399,300,205,203,404,405,202,301,106,304,401,201,402,403,303],
["888888",null,4,null,5,3,6,2,1,1,7,2,null,null,null,null,null,null,null,null,null,null,null],
["999999",null,2,null,1,0,null,null,3,2,3,null,null,null,null,null,null,null,null,null,null,null,null],
["111111",1,3,null,5,4,10,7,1,2,null,null,null,null,null,null,null,1,null,null,null,null,null],
["222222",null,1,null,1,1,3,1,null,null,null,null,3,null,null,null,null,null,null,null,null,null,null],
[333333,2,8,null,8,9,10,8,2,5,8,8,null,7,8,null,1,null,null,6,null,6,null],
[444444,null,3,null,2,2,8,3,1,1,null,null,2,6,null,null,null,null,null,null,null,null,null],
[555555,null,2,null,2,2,8,3,2,1,null,null,2,3,null,null,null,null,null,null,null,null,null],
[666666,null,2,null,2,2,7,3,1,2,null,null,2,8,null,null,null,null,null,null,null,null,null],
[777777,null,2,null,2,2,8,3,1,1,null,null,2,7,null,null,null,null,2,null,null,null,null]
]

这看起来是正确的。但是我注意到从第 5 行开始,ps_job_serial_number 周围不再有引号。我想知道这是否是让它变得疯狂的原因?

来自 Chrome 控制台的错误消息是:

Uncaught Error: Not an array
(anonymous function) 
drawChart

更新 #2 将查询更改为仅返回在 ps_job_serial_num 周围有引号的行(如果导致错误,则进行故障排除)。它没有任何区别。错误保持不变:

Uncaught Error: Not an array
(anonymous function) 
drawChart
4

1 回答 1

0

在你的 PHP 中试试这个:

<?php
    $mysqli = new mysqli("localhost", "user", "password", "database");
    $query = "CALL new_procedure()";
    $result = $mysqli->query($query);

    $results_array = Array(Array());
    $fillKeys = true;
    while ($row = $result->fetch_assoc()) {
        $temp = Array();
        foreach ($row as $key => val) {
            if ($fillKeys) {
                $results_array[0][] = $key;
            }
            $temp[] = $val;
        }
        $results_array[] = $temp;
        $fillKeys = false;
    }
    $jsontable = json_encode($results_array, JSON_NUMERIC_CHECK);
    echo $jsontable;
?>

并在您的 javascript 中,更改 DataTable 构造以使用该#arrayToDataTable方法(如果您想以这种特定格式放置数据,这是必需的 - 如果您想使用标准构造函数,您必须知道每列的数据类型是什么):

[编辑 - 在JSON.parse下面添加]

var data = google.visualization.arrayToDataTable(JSON.parse(jsonData));
于 2013-11-13T23:00:06.213 回答