使用PHP MySQL的每日活动图表

时间:2017-01-15 10:48:21

标签: php mysql chart.js

我有两个mysql表writersposts

writers:
--------------------------
id | name | ....some more..
--------------------------

posts:
-------------------------------------------------------------
post_id | user_id | contents | date_posted | .... some more...
--------------------------------------------------------------

如何使用PHP在折线图(如ChartJS)中生成每个用户的日常活动。结果应该像我附上的图像一样。

enter image description here

2 个答案:

答案 0 :(得分:2)

对您的解决方案使用这样的数据库查询(并根据您的需要进行更正):

Select count(post_id) as post_cnt, w.name as author, date_posted 
from posts as p 
join writers w on p.user_id = w.id 
where p.user_id = #user_id 
group by date_posted;

对于PHP使用一些带缓存的框架,并将带有json格式的数据发送到ajax响应,对你的JQuery插件,我使用这样的谷歌图表,但如果用户真的需要它(如点击或页面滚动事件)我加载它等)

答案 1 :(得分:0)

感谢@JanSršeň。他建议我使用下面的查询:

$query = sprintf("SELECT count(`p`.`user_id`) as `post_cnt`, `w`.`id` as `author`,date_posted from posts as p join writer w on `p`.`user_id` =  `w`.`id` where `p`.`user_id` = '$user_id' group by `date_posted`");

并生成一个json文件:

[{"post_cnt":"3","author":"1001","date_posted":"2017-01-08 14:26:14"},{"post_cnt":"1","author":"1001","date_posted":"2017-01-12 08:37:55"},{"post_cnt":"4","author":"1001","date_posted":"2017-01-12 23:27:23"},{"post_cnt":"1","author":"1001","date_posted":"2017-01-12 23:29:15"},{"post_cnt":"2","author":"1001","date_posted":"2017-01-12 23:30:27"},{"post_cnt":"1","author":"1001","date_posted":"2017-01-12 23:31:04"},{"post_cnt":"1","author":"1001","date_posted":"2017-01-12 23:33:03"}]

然后,我在ChartJS中使用这些数据生成折线图!

谢谢!