如何使用 PHP 和 MySQL 显示网站上的活动最多的日期和时间?
我想在我的网站上制作一个条形图,显示一周中的哪几天和一天中的哪些时间有最多的活动/登录。
所以我想知道是否有人可以给我一些关于如何做到这一点的提示?我应该如何组织数据库表、收集哪些数据以及如何使用 PHP 将记录为条形图的数据呈现出来。
I want to make a bar diagram on my website that shows what days of the week and times-of-day has the most activity/logins.
So i'm wondering if someone could give me some tips on how they would do this? How i should organize the database table(s), what data to gather, and how i can present the data recorded as a bargraph using PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用服务器日志进行可视化可能更容易。处理 ( http://www.processing.org/ ) 很擅长做这样的事情,或者尝试 http://processingjs.org/ 对于非 Java 版本。
不过,为了解决您的问题,正如前面提到的,存储每次访问的数据时间(如果您进行的是唯一访问,也可能存储 IP),然后编写一个脚本来提取记录并计算每小时的总计。您可以使用彩色 div,并将高度设置为点击次数(或分数)。实际上这基本上是一个点击计数器,不是吗?
是的,我的建议是检查一下处理过程,它很容易上手并产生漂亮的结果。
Might be easier to do a visualisation using your server logs. Processing ( http://www.processing.org/ ) is good at things like that, or try http://processingjs.org/ for a non Java version.
To address your question though, as has been mentioned, store the data time for each visit (and maybe IP as well if you're after unique visits) then write a script to pull the records and do a total for each hour. You could use coloured divs, with the height set to the number (or a fraction) of the hits. Actually this is basically a hit counter isn't it.
Yeah, my advice would be to check out processing though, it's easy to pick up and produces nice looking results.
如果您必须自己做而不是使用预先存在的软件包。从小处开始。只需创建一个表来记录每个请求、请求者、时间、引用者以及您想要的任何信息。
记录数据后,如果您仍然不想使用 Google Charts 之类的东西,您可以尝试使用 css 和 html 制作自己的简单 PHP 图表。
条形图可能非常简单,但需要一些数学知识才能正确绘制。最简单的方法是将它们翻转到一侧。您通常将它们视为建筑物,但在本例中您希望将它们视为蠕虫状的管道。管子的长度可以代表点击量、综合浏览量、访问量等。
我将为整个图表定义一个固定宽度,并更改条形的宽度以表示最大值的百分比。因此,如果最大值为 1000,而该条形为 500,则您可以将该条形设置为图表宽度的 50%。
我希望这能为您指明正确的方向。做这样的事情对于学习新概念非常有用。
If you must do it yourself and not use a pre-existing package. Start small. Simply create one table that logs each request, who made it, the time, the referer, whatever info you want.
Once you have the data logged, if you still don't want to use something like Google Charts you can try making your own simple PHP charts with css and html.
Bar diagrams can be pretty easy, but require some math to get right. The easiest way is to flip them on their side. You normally see them like buildings, but you want in this case to see them as worm-like tubes. The length of the tube can then represent the number of hits, pageviews, visits, so on.
I would define a fixed width for the whole chart and change the width of the bars to represent a percentage of the max value. So if the max value is 1000, and this bar is 500, you would make the bar 50% of the width of your chart.
I hope this points you in the right direction. Doing something like this can be great for learning new concepts.
也许最好的方法是为每次访问或登录存储日期时间戳。然后,您可以使用巧妙的 SQL 查询对信息进行分组(一周中的几天、一天中的时间或其他任何内容)。
因此,数据库表可能只需要主键和访问的时间戳,尽管您当然也可以存储有关用户或操作的信息。
为了进行演示,请尝试 Google Charts (http://code.google.com/apis/chart/) 或基于 javascript 的图表库(有几个,我在 Highcharts http://www.highcharts.com/)
ps:既然您将这篇文章标记为 MySQL,我假设这是您的数据库选择。这是您可以使用的表创建语句。
Probably the best way to do this is to store a datetime stamp for each visit or login. Then, you can use clever SQL queries to group the information however you want (days of week, times of day, or anything else.)
So, the database table would likely only need a primary key and the timestamp of the visit, though you could definitely store information about the user or the action as well.
For presentation, try either Google Charts (http://code.google.com/apis/chart/) or a javascript-based charting library (There are several out there, I've had good experience with Highcharts http://www.highcharts.com/)
ps: Since you flagged this post as MySQL, I'm assuming that's your database of choice. Here is a table creation statement you can use.