更新問題
谷歌 chart 問題
官方文件 https://google-developers.appspot.com/chart/interactive/docs/gallery/columnchart
$.getJSON('XXX&m=' + m, function(json) {
var data = new google.visualization.DataTable(json);
data.addColumn('string', '');
data.addColumn('number', '');
data.addRows(json);
console.log(json);
php
$chartData = $pdo->query(
"SELECT DISTINCT
FROM_UNIXTIME(add_time, '%m/%d') as add_time,
count(view_id) as viewTime
FROM `view`
GROUP BY `add_time` "
);
foreach ($chartData as $key) {
if (substr($key['add_time'] ,0 ,2) == $_GET['m']) {
$data[] = array(
$key['add_time'], (int) $key['viewTime']
);
}
}
echo json_encode($data ,JSON_UNESCAPED_SLASHES);
我每一筆 view 都有一個 add_time 時間戳
我上面是把時間戳轉換為 mm/dd 格式,然後再透過 $_GET['m'] 帶來的日期,例如 05
output[["11/01",1],["11/02",1],["11/03",1],["11/05",1]]
但其實 11/01
,11/02
, 11/03
, 11/05
都是多筆的
透過 count(view_id) as viewTime
算出來都是一筆~
所以我想不到怎麼算出每個日期共有幾筆?
這問題好像跟 google chart 無關
只是資料庫查詢語法的問題
$chartData = $pdo->query(
"SELECT DISTINCT
FROM_UNIXTIME(add_time, '%m/%d') as add_date,
count(view_id) as viewTime
FROM `view`
GROUP BY `add_date` "
);
把as add_time
改成as add_date
並把GROUP BY add_time
改成GROUP BY add_date
以避免資料庫用原本的「add_time」欄位做 GROUP