如何使我的 JSON 动态看起来与下面示例中的 google 图表相同?
how can i make my JSON dynamically looks the same as in the example below for google charts?
首先检查这个例子:https://jsfiddle.net/hisham91/ws6mjkps/
CREATE TABLE `googlecharts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`item_code` VARCHAR( 100 ) NOT NULL ,
`sold_date` DATE NOT NULL ,
`serial_no` INT NOT NULL
) ENGINE = MYISAM ;
INSERT INTO `googlecharts` VALUES (1, 'scratch card 3 USD', '2014-11-21', 22313);
INSERT INTO `googlecharts` VALUES (2, 'scratch card 10 USD', '2014-11-21', 90123);
INSERT INTO `googlecharts` VALUES (3, 'scratch card 4 USD', '2014-11-21', 47723);
INSERT INTO `googlecharts` VALUES (4, 'scratch card 3 USD', '2014-12-16', 22263);
INSERT INTO `googlecharts` VALUES (5, 'scratch card 10 USD', '2014-12-16', 11123);
INSERT INTO `googlecharts` VALUES (6, 'scratch card 4 USD', '2014-12-16', 45113);
INSERT INTO `googlecharts` VALUES (7, 'scratch card 3 USD', '2015-01-01', 23333);
INSERT INTO `googlecharts` VALUES (8, 'scratch card 10 USD', '2015-01-01', 34543);
INSERT INTO `googlecharts` VALUES (9, 'scratch card 4 USD', '2015-01-01', 41243);
INSERT INTO `googlecharts` VALUES (10, 'scratch card 3 USD', '2015-02-18', 23123);
INSERT INTO `googlecharts` VALUES (11, 'scratch card 10 USD', '2015-02-18', 34443);
INSERT INTO `googlecharts` VALUES (12, 'scratch card 4 USD', '2015-02-18', 45643);
INSERT INTO `googlecharts` VALUES (13, 'scratch card 3 USD', '2015-03-17', 23433);
INSERT INTO `googlecharts` VALUES (14, 'scratch card 10 USD', '2015-03-17', 34223);
INSERT INTO `googlecharts` VALUES (15, 'scratch card 4 USD', '2015-03-17', 45123);
其次,我需要知道如何让我的 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 中的示例,我想您会了解我正在尝试做的事情,如果可以的话请帮助我..我是 Whosebug 的新手,这是我的第一个已订购 问题 XD 感谢大家抽出时间。
竖起大拇指 !! ^_^
亲爱的,我通过名为“pivot”pivot-table 或 pivot-view 的东西实现了这一点通过互联网搜索...如果有人想这样做告诉我,我会向你解释。
谢谢大家。
首先检查这个例子:https://jsfiddle.net/hisham91/ws6mjkps/
CREATE TABLE `googlecharts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`item_code` VARCHAR( 100 ) NOT NULL ,
`sold_date` DATE NOT NULL ,
`serial_no` INT NOT NULL
) ENGINE = MYISAM ;
INSERT INTO `googlecharts` VALUES (1, 'scratch card 3 USD', '2014-11-21', 22313);
INSERT INTO `googlecharts` VALUES (2, 'scratch card 10 USD', '2014-11-21', 90123);
INSERT INTO `googlecharts` VALUES (3, 'scratch card 4 USD', '2014-11-21', 47723);
INSERT INTO `googlecharts` VALUES (4, 'scratch card 3 USD', '2014-12-16', 22263);
INSERT INTO `googlecharts` VALUES (5, 'scratch card 10 USD', '2014-12-16', 11123);
INSERT INTO `googlecharts` VALUES (6, 'scratch card 4 USD', '2014-12-16', 45113);
INSERT INTO `googlecharts` VALUES (7, 'scratch card 3 USD', '2015-01-01', 23333);
INSERT INTO `googlecharts` VALUES (8, 'scratch card 10 USD', '2015-01-01', 34543);
INSERT INTO `googlecharts` VALUES (9, 'scratch card 4 USD', '2015-01-01', 41243);
INSERT INTO `googlecharts` VALUES (10, 'scratch card 3 USD', '2015-02-18', 23123);
INSERT INTO `googlecharts` VALUES (11, 'scratch card 10 USD', '2015-02-18', 34443);
INSERT INTO `googlecharts` VALUES (12, 'scratch card 4 USD', '2015-02-18', 45643);
INSERT INTO `googlecharts` VALUES (13, 'scratch card 3 USD', '2015-03-17', 23433);
INSERT INTO `googlecharts` VALUES (14, 'scratch card 10 USD', '2015-03-17', 34223);
INSERT INTO `googlecharts` VALUES (15, 'scratch card 4 USD', '2015-03-17', 45123);
其次,我需要知道如何让我的 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 中的示例,我想您会了解我正在尝试做的事情,如果可以的话请帮助我..我是 Whosebug 的新手,这是我的第一个已订购 问题 XD 感谢大家抽出时间。 竖起大拇指 !! ^_^
亲爱的,我通过名为“pivot”pivot-table 或 pivot-view 的东西实现了这一点通过互联网搜索...如果有人想这样做告诉我,我会向你解释。 谢谢大家。