[英]PHP MySQL: count views per month for jQuery charts
i work with jQuery
chart.js for show view counter by month
using PHP
and MySql
. 我使用jQuery
chart.js使用PHP
和MySql
month
显示视图计数器。 I insert/Put each views page to MySql like this : 我像这样将每个视图页面插入/放置到MySql中:
| id | ip | page | date(timestamp) |
| 1 | 138.86.20.20 | test.php | 1375710823 |
| 2 | 100.86.123.10 | test.php | 1380206563 |
for chart.js : 对于chart.js:
var data = {
labels: ["january","February", "March", "April", "May", "June", "July"],
datasets: [{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
data: [150,59, 90, 81, 56, 55, 40]
}, {
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,1)",
data: [20,48, 40, 59, 500, 127, 100]
}]
}
var options = {animation :true};
//Get the context of the canvas element we want to select
var c = $('#daily-chart');
var ct = c.get(0).getContext('2d');
var ctx = document.getElementById("daily-chart").getContext("2d");
//Run function when window resizes
$(window).resize(respondCanvas);
function respondCanvas() {
c.attr('width', jQuery("#daily").width());
c.attr('height', jQuery("#daily").height());
//Call a function to redraw other content (texts, images etc)
myNewChart = new Chart(ct).Bar(data, options);
}
//Initial call
respondCanvas();
Now, I need to count views by/per month for chart.js data ouput using json format. 现在,我需要使用json格式按月/按月统计chart.js数据输出的视图。
[150,59, 90, 81, 56, 55, 40,12,54,65,365,2]
for 12 month strat @january. [150,59, 90, 81, 56, 55, 40,12,54,65,365,2]
12月strat @january。
How to crate this?! 如何包装这个?
NOTE: charts datasets have two color : 1- unique user 2- all user 注意:图表数据集有两种颜色:1-唯一用户2-所有用户
You could use a query to group the visits by month: 您可以使用查询按月对访问进行分组:
SELECT COUNT(*), MONTH(date(timestamp)), YEAR(date(timestamp))
FROM table
GROUP BY MONTH(FROM_UNIXTIME(date(timestamp)), YEAR(FROM_UNIXTIME(date(timestamp))
Which will return the number of visits, with the year and month they correspond to. 它将返回访问次数以及它们对应的年份和月份。
\\EDIT\\ \\编辑\\
To loop through and print each month's value, use a mysqli_query and WHILE
in PHP: 要遍历并打印每个月的值,请在PHP中使用mysqli_query和WHILE
:
$query = mysqli_query($con, "THE QUERY ABOVE ^^");
while($row = mysqli_fetch_array($query {
echo $row['date(timestamp)'].",";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.