Php 如何使我的JSON动态地看起来与下面谷歌图表的示例相同?
首先,检查此示例:Php 如何使我的JSON动态地看起来与下面谷歌图表的示例相同?,php,json,for-loop,charts,google-visualization,Php,Json,For Loop,Charts,Google Visualization,首先,检查此示例: 其次,我需要知道如何通过知道下面的代码是针对数据库的,从而使我的JSON数据看起来与此相同: <?php $con=mysql_connect("localhost","root","") or die("Problem while connecting to the database!"); mysql_select_db("charts", $con); $sth = mysql_query("SELECT item_code,serial_no,dat
其次,我需要知道如何通过知道下面的代码是针对数据库的,从而使我的JSON数据看起来与此相同:
<?php
$con=mysql_connect("localhost","root","") or die("Problem while connecting to the database!");
mysql_select_db("charts", $con);
$sth = mysql_query("SELECT item_code,serial_no,date FROM googlecharts group by item_code");
$rows = array();
$flag = true;
$table = array();
$table['cols'] = array(
//i guess i need a loop through the first column to get all items and make them labels
array('label' => 'item code', 'type' => 'string'),
array('label' => 'sum of serial numbers', 'type' => 'number'),
array('label' => 'sold month', 'type' => 'number')
);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$temp = array();
$temp[] = array('v' => (string) $r['item_code']);
$temp[] = array('v' => (int) $r['serial_no']);
$temp[] = array('v' => (int) $r['sold_date']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
?>
通过在jsfiddle.net中看到这个例子,我想你会对我的想法有所了解,如果可以的话,请帮助我。。我是stackoverflow的新手,这是我第一个订购的问题XD,谢谢大家。
竖起大拇指^ ^亲爱的,我通过一种叫做“pivot”的方式在互联网上搜索数据透视表或数据透视视图,实现了这一点。。。如果有人想这么做,告诉我我会向你解释的。 谢谢大家
<?php
$con=mysql_connect("localhost","root","") or die("Problem while connecting to the database!");
mysql_select_db("charts", $con);
$sth = mysql_query("SELECT item_code,serial_no,date FROM googlecharts group by item_code");
$rows = array();
$flag = true;
$table = array();
$table['cols'] = array(
//i guess i need a loop through the first column to get all items and make them labels
array('label' => 'item code', 'type' => 'string'),
array('label' => 'sum of serial numbers', 'type' => 'number'),
array('label' => 'sold month', 'type' => 'number')
);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$temp = array();
$temp[] = array('v' => (string) $r['item_code']);
$temp[] = array('v' => (int) $r['serial_no']);
$temp[] = array('v' => (int) $r['sold_date']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
?>