douzhuiqing1151 2015-10-06 10:25
浏览 55
已采纳

将MySQL结果转换为Google Graphs的JavaScript数组

I'm running a query that fetches some data back which need to be converted to a JavaScript array for using a Google Graph.

Here is the query:

SELECT 
sub.supplier_name,
(
    SELECT ROUND(AVG(sp.progress), 2) as P1 
    FROM `sub_performancemeasures` AS sp
    INNER JOIN `submissions` AS sub ON sub.id = sp.sub_id
    WHERE `period` = '1'
) AS p1,
(
    SELECT ROUND(AVG(sp.progress), 2) as P2 
    FROM `sub_performancemeasures` AS sp
    INNER JOIN `submissions` AS sub ON sub.id = sp.sub_id
    WHERE `period` = '2'
) AS p2,
(
    SELECT ROUND(AVG(sp.progress), 2) as P3
    FROM `sub_performancemeasures` AS sp
    INNER JOIN `submissions` AS sub ON sub.id = sp.sub_id
    WHERE `period` = '3'
) AS p3,
(
    SELECT ROUND(AVG(sp.progress), 2) as P4
    FROM `sub_performancemeasures` AS sp
    INNER JOIN `submissions` AS sub ON sub.id = sp.sub_id
    WHERE `period` = '4'
) AS p4,
(
    SELECT ROUND(AVG(sp.progress), 2) as P5
    FROM `sub_performancemeasures` AS sp
    INNER JOIN `submissions` AS sub ON sub.id = sp.sub_id
    WHERE `period` = '5'
) AS p5
FROM `submissions` AS sub
INNER JOIN `sub_performancemeasures` AS sp ON sub.id = sp.sub_id
INNER JOIN `performance_measures` AS pm ON sp.pm_id = pm.id
INNER JOIN `kpis` ON kpis.id = pm.kpi_id
GROUP BY sub.supplier_name

This query produces the following array:

Array ( 
  [0] => Array ( 
          [sub] => Array ( 
                           [supplier_name] => C Spencer Ltd 
                         ) 
           [0]  => Array ( 
                           [p1] => -11.43 
                           [p2] => 36.67 
                           [p3] => 
                           [p4] => 
                           [p5] => 
                         ) 
               ) 
   [1] => Array ( 
          [sub] => Array ( 
                           [supplier_name] => Supplier 2 
                         ) 
           [0]  => Array ( 
                           [p1] => 15.21 
                           [p2] => -6.44 
                           [p3] => 
                           [p4] => 65.41
                           [p5] => 
                         ) 
               ) 
      ) 

I then need to take this query and convert it to fit the following JavaScript array:

[
   ['Period', 'Allen', 'Tom', 'Sim'],
   [1, null, null, -100],
   [2, 3, null, null],
   [3, 3, null, null],
   [4, null, 2, null],
   [5, 3, 100, null],
]

The data currently in the string is example data. I need the array to look like this:

[
   ['Period', 'C Spencer Ltd', 'Supplier 2'],
   [1, -11.43, 15.21],
   [2, 36.67, -6.44],
   [3, null, null],
   [4, null, 65.41],
   [5, null, null],
]

This is what I've tried so far:

$str1="[['Period',";
foreach($avgSupScores as $supplier){

    $str1 .= "'".$supplier['sub']['supplier_name']."', ";
    //$str2 .= "1, ".$periods[0]['p1']."";
}
$str1 = rtrim($str1, ", ");

$str1 .= "],[";



echo $str1;

This results in:

[['Period','C Spencer Ltd', 'Supplier 2'],[

The first line is OK but I'm not sure where to go from here to get what I need, nor am I sure this is even the right/best approach.

  • 写回答

1条回答 默认 最新

  • douyueqing1530 2015-10-06 10:37
    关注

    Use the following code it will help you. You have to separate the keys and values and then merge.

    <?php
    $con=mysql_connect("localhost","Uname","pwd") or die("Failed to connect with database!");
    mysql_select_db("db", $con); 
    
    $fields = mysql_query("SELECT * FROM chart_data");
    
    while($r = mysql_fetch_assoc($fields)) {
    $arr2=array_keys($r);
    $arr1=array_values($r);
    
    }
    
    for($i=0;$i<count($arr1);$i++)
    {
        $chartData[$i]=array((string)$arr2[$i],intval($arr1[$i]));
    }
    echo "<pre>";
    $data=json_encode($chartData);  //pass this $data in the google graph
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算