2017-01-15 3 views
1

私は2つのmysqlテーブルwriterspostsを持っています。PHPを使用した毎日の活動チャート

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

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

どのように私はPHPを使用して(ChartJSのような)折れ線グラフで、各ユーザーの日々の活動を生成することができます。結果は、私が添付したイメージのようになります。 PHPの場合

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; 

キャッシングでいくつかのフレームワークを使用し、Ajaxの応答にJSON形式でデータを送信します。あなたの解決のために、このようなDBクエリ(とあなたのニーズに合わせて、それを修正)

enter image description here

+0

ペンと紙? – Strawberry

答えて

2

使用あなたのJQueryプラグインに、私はこのようなGoogle Chartを使用しますが、ユーザーが本当に必要な場合(クリックやページスクロールイベントなど)

+0

ありがとう、@ジャン! – SundayaMizo

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にこれらのデータを使用!

ありがとうございました!

関連する問題