gpt4 book ai didi

php - 谷歌图表 + MySQL/json

转载 作者:行者123 更新时间:2023-11-29 03:39:00 24 4
gpt4 key购买 nike

这几天我一直在努力让它工作。我想要的是用 googlecharts 创建柱形图/条形图。并从我的数据库中动态生成。

我的查询:

SELECT gs.league as league
, (Select Count(*) From gamestats WHERE season=gs.season AND league=gs.league AND event=1) as one
, (Select Count(*) From gamestats WHERE season=gs.season AND league=gs.league AND event=2) as two
, (Select Count(*) From gamestats WHERE season=gs.season AND league=gs.league AND event=3) as three
, (Select Count(*) From gamestats WHERE season=gs.season AND league=gs.league AND event=4) as four
, (Select Count(*) From gamestats WHERE season=gs.season AND league=gs.league AND event=5) as five
, (Select Count(*) From gamestats WHERE season=gs.season AND league=gs.league AND event=6) as six
FROM gamestats gs WHERE gs.season=2013 AND gs.league < 3 GROUP by gs.league

如果我只是尝试 json_encode($result) 它会给我错误的 json 格式。这是我现在得到的:

[{"league":"2","one":"9","two":"3","three":"9","four":"2","five":"8","six":"12"},{"league":"4","one":"1","two":"0","three":"0","four":"1","five":"1","six":"2"}]

那不会产生任何东西。在 GoogleChart 示例中,json 是这样的:

{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}

如何从我的查询中获取那种 json 格式?谢谢!

最佳答案

我前段时间也遇到过这个问题。这是我修复它的方法,我想你会明白的。

$data = $this->stats_model->get_flights_data(); // this is the model

$label = array();

array_push($label, array("id" => "", "label" => "Airline", "pattern" => "", "type" => "string"));
array_push($label, array("id" => "", "label" => "Amount", "pattern" => "", "type" => "number"));

$rows = array();
foreach($data as $result) {
array_push($rows, array("c" => array(array("v" => $result['airline'], "f" => null), array("v" => (int)$result['amount'], "f" => null ))));
}

$cols = array("cols" => $label, "rows" => $rows);
echo json_encode($cols);

这是我的模型:

$query = "SELECT count(flight_number) AS `amount`, airline from flights_database WHERE flight_date = CURDATE() AND airline != '' GROUP by airline HAVING count(flight_number)>0 ORDER by count(flight_number) DESC, airline DESC LIMIT 10";

$q = $this->db->query($query);
$data = $q->result_array();

return $data;

希望这能让您有所了解!

关于php - 谷歌图表 + MySQL/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16950290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com