PHP/MySQL - 富达计划实施
我想在我的网站上实施一个保真计划,类似于 stackoverflow 上的计划。
我希望能够为连续 30 天访问我的网站的用户提供某种奖励。
[MySQL] 最好的表架构是什么?
[PHP] 我应该使用什么样的算法来优化这个任务?
I would like to implement a fidelity program, similar to the one on stackoverflow, on my website.
I want to be able to give some kind of reward to users who have visited my website for 30 days in a row.
[MySQL] What would be the best table architecture?
[PHP] What kind of algorithm should I use to optimize this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 @Matt H. 提倡的方法相比,我更喜欢数据库中更多的原始数据。创建一个表,记录所有站点登录(或者,如果您愿意,还可以记录新会话启动)及其时间和日期:
只需在登录时将 UserId 插入表中即可。当然,我对您的数据库做了一些假设,但我认为您能够适应。
然后,检查前 30 天中每一天的离散登录情况:
如果结果是 30,则说明您很幸运。
我承认我有一段时间没有接触过 MySQL(最近主要在 SQL Server 和 PostgreSQL 上工作),所以如果语法有点不对劲,我深表歉意。不过,我希望这个概念有意义。
I prefer more raw data in the database than the approach that @Matt H. advocates. Make a table that records all logins to the site (or, if you prefer, new session initiations) along with their time and date:
Just insert the UserId into the table on login. I, of course, made some assumptions about your database, but I think you will be able to adapt.
Then, to check for discrete logins for each of the preceding thirty days:
If the result is 30, you're golden.
I will admit that I haven't touched MySQL in a while (working mainly on SQL Server and on PostgreSQL lately), so if the syntax is off a bit, I apologize. I hope the concept makes sense, though.
根据您上面的描述,这可以通过一张表相当简单地完成。
表格就是这样。 :)
该算法分为三个部分:
当用户登录时,一旦经过验证...
1) 检查用户 LAST_CONSECUTIVE_DAY。如果 LAST_CONSECUTIVE_DAY 是今天,则不执行任何操作。如果 LAST_CONSECUTIVE_DAY 是昨天,请将 LAST_CONSECUTIVE_DAY 设置为今天的日期。否则,将 FIRST_CONSECUTIVE_DAY 和 LAST_CONSECUTIVE_DAY 设置为今天的日期。
2)使用 TIMESTAMPDIFF 来按 DAY 单位比较 LAST_CONSECUTIVE_DAY 和 FIRST_CONSECUTIVE_DAY。如果返回 30,则继续执行步骤 3,否则继续执行应用程序。
3) 您的用户连续 30 天每天都访问该网站!恭喜!检查 HAS_BEEN_REWARDED 看看他们以前是否做过,如果仍然错误,给他们奖励并将 HAS_BEEN_REWARDED 标记为 true。
From your description above, this could be accomplished fairly simply and with one table.
Thats it for the table. :)
The algorithm is in three parts:
When a user logs in, once they have been verified...
1) Check the users LAST_CONSECUTIVE_DAY. If the LAST_CONSECUTIVE_DAY is today, do nothing. If the LAST_CONSECUTIVE_DAY is yesterday, set LAST_CONSECUTIVE_DAY to todays date. Otherwise, set FIRST_CONSECUTIVE_DAY and LAST_CONSECUTIVE_DAY to todays date.
2) Use TIMESTAMPDIFF to compare LAST_CONSECUTIVE_DAY and FIRST_CONSECUTIVE_DAY by the DAY unit. If it returns 30 go on to step 3, otherwise move on with the application.
3) Your user has visited the website every single day for 30 days in a row! Congratulations! Check HAS_BEEN_REWARDED to see if they have done it before, if still false give them a prize and mark HAS_BEEN_REWARDED as true.