PHP/MySQL - 富达计划实施

发布于 2024-10-16 10:03:25 字数 148 浏览 0 评论 0原文

我想在我的网站上实施一个保真计划,类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-10-23 10:03:25

与 @Matt H. 提倡的方法相比,我更喜欢数据库中更多的原始数据。创建一个表,记录所有站点登录(或者,如果您愿意,还可以记录新会话启动)及其时间和日期:

CREATE TABLE LoginLog (
    UserId INT NOT NULL REFERENCES Users (UserId),
    LoginTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)

CREATE INDEX IX_LoginLog USING BTREE ON LoginLog (UserId ASC, LoginTime DESC)

只需在登录时将 UserId 插入表中即可。当然,我对您的数据库做了一些假设,但我认为您能够适应。

然后,检查前 30 天中每一天的离散登录情况:

SELECT COUNT(*)
FROM (SELECT   DATE(log.LoginTime) AS LoginDate,
               COUNT(*) AS LoginCount
      FROM     LoginLog log
      WHERE    log.LoginTime >= DATE(DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 30 DAYS))
      GROUP BY LoginDate
      HAVING   COUNT(*) > 0) a

如果结果是 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:

CREATE TABLE LoginLog (
    UserId INT NOT NULL REFERENCES Users (UserId),
    LoginTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)

CREATE INDEX IX_LoginLog USING BTREE ON LoginLog (UserId ASC, LoginTime DESC)

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:

SELECT COUNT(*)
FROM (SELECT   DATE(log.LoginTime) AS LoginDate,
               COUNT(*) AS LoginCount
      FROM     LoginLog log
      WHERE    log.LoginTime >= DATE(DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 30 DAYS))
      GROUP BY LoginDate
      HAVING   COUNT(*) > 0) a

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.

梦醒时光 2024-10-23 10:03:25

根据您上面的描述,这可以通过一张表相当简单地完成。

  • |身份证 |表PK,自动递增
  • |电子邮件 |网站访问者唯一 ID。表面上是一封电子邮件,但也可以是唯一标识访问者的任何数据
  • |连续第一天 |时间戳
  • | LAST_CONSECUTIVE_DAY |时间戳
  • | HAS_BEEN_REWARDED | bool,默认 false(0)

表格就是这样。 :)

该算法分为三个部分:
当用户登录时,一旦经过验证...

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.

  • | ID | Table PK, auto-incrementing
  • | EMAIL | website visitor unique ID. Ostensibly an email, but could be any piece of data that uniquely ID's the visitor
  • | FIRST_CONSECUTIVE_DAY | timestamp
  • | LAST_CONSECUTIVE_DAY | timestamp
  • | HAS_BEEN_REWARDED | bool, default false(0)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文