每天授予用户一分
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这比 PHP 更适合数据库。添加一个带有唯一索引
(user_id,login_date)
的表users_points
。示例数据:然后在每次登录时,标记用户已在该日期登录(如果该行已经存在,索引将防止重复):
并获取点数:
这也很好,您有一个列表用户登录后的天数,以防您更改条件并想要重新计数。
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:Then on every login, mark that the user has logged in on that date (if the row already exists, the index will prevent duplication):
And to get the number of points:
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 aDATE
, 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:
insert ignore
select count
users
.使用单独的字段来保留将积分添加到用户帐户的日期。如果今天没有发生这种情况 - 添加一个(或多个)点并更新字段。
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.