每天授予用户一分

发布于 2024-09-17 10:59:06 字数 886 浏览 4 评论 0原文

使用 PHP/mySQL,用户每天都会被授予其会员帐户一个整数积分。我将用来确定是否应授予分数的数据是这些 mysql 字段:创建日期(时间戳)和上次登录(UNIX 时间)。

授予这些积分的过程是在用户登录时确定的。我的问题是,确定自上次登录以来已经过去了多少天的最有效方法是什么?其次,如果用户每天登录,如何判断是否已经过了24小时并给予积分?过去的天数等于给出的分数(每天 1 分)。

目前我正在使用这段代码:

/*
** Updates Points based on days since last visit
*/
static function UpdatePoints($username)
{
    $getlog = System::$mySQL->Select("lastLog, creation FROM users WHERE username='$username'");
    $log = System::$mySQL->Fetch($getlog);

    $offset = (TIME() - $log['lastLog']) / 86400;  // 24hrs
    $lastlog = round($offset); // in days

    if($lastlog > 0)
    {
        System::$mySQL->Update("users SET points=points+".$lastlog." WHERE username='$username'");
    }
}

除了标记之外,很明显我的代码是短视的。如果用户每天登录一次,他们不会获得积分。因此,我还必须使用创建日期字段来确定正确的方法。我今天实在是想不通,有什么建议吗?谢谢。

Using PHP/mySQL, a user is granted a single integer point to their member account each day. The data I will use to determine if a point should be granted are these mysql fields: Creation Date (timestamp) and Last Login (UNIX TIME).

The procedure for granting these points is determined when the user logs in. My question is, what's the most efficient way of determining how many days have passed since the last login? Secondly, if a user logs in each day, how do I determine if 24 hours has passed and a point is to be granted? Days past equates to the points given (1 per day).

Currently I am using this code:

/*
** Updates Points based on days since last visit
*/
static function UpdatePoints($username)
{
    $getlog = System::$mySQL->Select("lastLog, creation FROM users WHERE username='$username'");
    $log = System::$mySQL->Fetch($getlog);

    $offset = (TIME() - $log['lastLog']) / 86400;  // 24hrs
    $lastlog = round($offset); // in days

    if($lastlog > 0)
    {
        System::$mySQL->Update("users SET points=points+".$lastlog." WHERE username='$username'");
    }
}

Markup aside, it's obvious my code is shortsighted. If the user logs in once everyday, they do not gain a point. Therefore I must determine the correct method for doing so using the Creation Date field as well. I just can't wrap my head around it today, any suggestions? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

叫思念不要吵 2024-09-24 10:59:06

这比 PHP 更适合数据库。添加一个带有唯一索引(user_id,login_date)的表users_points。示例数据:

 user_id | login_date
====================== 
 19746   | 2010-09-02
 19746   | 2010-09-03

然后在每次登录时,标记用户已在该日期登录(如果该行已经存在,索引将防止重复):

INSERT IGNORE INTO `users_points` (`user_id`,`login_date`) VALUES (19746,CURDATE())

并获取点数:

SELECT COUNT(*) FROM `users_points` WHERE `user_id` = 19746

这也很好,您有一个列表用户登录后的天数,以防您更改条件并想要重新计数。

user_id是一个INT,login_date是一个DATE,有一个可用的索引,所以insert和select都应该很快,而且即使数字很大,表也会相对较小用户数。

如果您坚持将用户分数存储在某个位置(或者您可能想将其与其他用户数据一起检索),您可以在登录时执行此操作:

  • 运行 insertignore
  • 运行 select count
  • 将结果保存在表users 的列中。

This is better suited for the database than for PHP. Add a table users_points, with unique index (user_id,login_date). Sample data:

 user_id | login_date
====================== 
 19746   | 2010-09-02
 19746   | 2010-09-03

Then on every login, mark that the user has logged in on that date (if the row already exists, the index will prevent duplication):

INSERT IGNORE INTO `users_points` (`user_id`,`login_date`) VALUES (19746,CURDATE())

And to get the number of points:

SELECT COUNT(*) FROM `users_points` WHERE `user_id` = 19746

This is also good that you have a list of days when the user has logged in, in case you change the criteria and want to do a recount.

user_id is an INT, login_date is a DATE, there's a usable index, so both insert and select should be quick, and the table will be relatively small even with a huge number of users.

In case you insist on having the user score stored in some place (or maybe you want to retrieve it together with other user data), you could do this on login:

  • run the insert ignore
  • run the select count
  • save the result in a column of table users.
柳絮泡泡 2024-09-24 10:59:06

使用单独的字段来保留将积分添加到用户帐户的日期。如果今天没有发生这种情况 - 添加一个(或多个)点并更新字段。

Use a separate field to keep the date when you added the point to user's account. If this happened not today - add a point (or several) and update a field.

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