[英]From MYSQL query to google charts
Let me start by saying, i'm not sure how to ask the right question on this, but here goes... 首先,我不确定如何对此提出正确的问题,但是这里...
I have a database of amounts that all have times attached to them. 我有一个数量数据库,上面都有时间。 for example four rows would look like: 例如,四行看起来像:
2 | 1000 | 2012-11-01 12:09:09
2 | 800 | 2012-11-01 12:10:21
10 | 900 | 2012-11-01 12:11:36
10 | 750 | 2012-11-01 12:12:46
The first column is the user. 第一列是用户。 I am wanting to out put this to a google line graph, but really struggling on how to set-up the query. 我想把它放到一个谷歌折线图上,但真的很难在如何设置查询。
The end result in the javascript array needs to looks like: javascript数组的最终结果需要如下所示:
data.addColumn('string', 'Date');
data.addColumn('number', 'User 2');
data.addColumn('number', 'User 10');
data.addRows([
['12:09', 1000, null],
['12:10', 800, null],
['12:11', 800, 900],
['12:12', 800, 750]
]);
Any help or steers more than welcome on this. 在此方面,任何帮助或引导都非常值得欢迎。 I'm thinking a few php loop, but a little stuck... 我在想一些php循环,但是有点卡住...
Put this at top of Your document: 将其放在文档的顶部:
<?php
$dsn = 'mysql:dbname=myDbName;host=127.0.0.1';
$user = 'dbUserName';
$password = 'dbPassword';
$pdo = new PDO($dsn, $user, $name);
$result = $pdo->query('select * from YourAmountsTableName');
foreach ($result as $row) {
$rows[] = "['{$row['time']}',{$row['ammount']},{$row['yourThirdValue']}]";
}
$rowsString = implode(',',$rows);
?>
Now In Your HTML document, or Your Javascript, echo the $rowsString in the place for rows: 现在,在您的HTML文档或Javascript中,在行处回显$ rowsString:
data.addRows([<?php echo $rowsString; ?>]);
adjust the code given for names, passwords ect. 调整给出的名称,密码等代码。 I have not tested the code, so please check if no quotes are missing or other misspellings. 我尚未测试代码,因此请检查是否没有引号丢失或其他拼写错误。 As for the Google Chart, https://developers.google.com/chart/interactive/docs/examples?hl=pl have great examples 至于Google图表, https://developers.google.com/chart/interactive/docs/examples?hl = pl就是很好的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.