Jump to content

reset automatically


almightyegg

Recommended Posts

how would i go about coding a reset (at midnight) when all my members get a daily stock of something...??
like they start with 100 points in something and they use them up during the day...then at midnight it goes back to 100 OR it they start with 100 and use X and then at midnight they will have X+100...

get me??
Link to comment
Share on other sites

In that case, why not use timestamps.  I'm not going to provide you with the code, just an idea.

1. A new user logs into your site the first day they register, you credit them with 100 points and store in the database the time of login.
2. The next time they login, you take the current timestamp and subtract the one stored in the datbase and round it down to the nearest day to work out the number of elapsed days.
3. You credit them with that number of elapsed day times 100 (up to the limit you've set) and then log the new timestamp for next time they log in.

Now for someone who's worked a lot with timestamps, dates, time functions etc.  This shouldn't bee too difficult, unfortunately that person isn't me.  :(

Regards
Huggie
Link to comment
Share on other sites

can anybody help me code this??

[tt]1. A new user logs into your site the first day they register, you credit them with 100 points and store in the database the time of login.
2. The next time they login, you take the current timestamp and subtract the one stored in the datbase and round it down to the nearest day to work out the number of elapsed days.
3. You credit them with that number of elapsed day times 100 (up to the limit you've set) and then log the new timestamp for next time they log in.
[/tt]
Link to comment
Share on other sites

I have no problems writing code for people :)

For number 1,you just need to add two columns- one for the points and one for timestamps. And in the query that creates their account, insert 100 to the points column and the returned value from [url=http://www.php.net/manual/en/function.time.php]time()[/url] to the "last_login" column (the one I said that hold timestamps).

2 and 3
[code]<?php
//Validate username and pass, start sessions etc etc'

$query = "SELECT ('last_login','points') FROM `Users_Table` WHERE username_column='$username'";
$result = mysql_query($query);
$row = mysql_fetch_array($result); //since there's only one user with the called $username no need to run a loop
if(time()-$row['last_login'] > 60*60*24)
{
$days = floor( (time()-$row['last_login']) / (60*60*24) );
$points = $row['points'] + $days*100;
$timestamp = $row['last_login'] + $days*60*60*24;
mysql_query("UPDATE `Users_Table` SET points='$points',last_login='$timestamp' WHERE username_column='$username'");
}

?>[/code]


Orio :)
Link to comment
Share on other sites

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/lordofth/public_html/welcome.php on line 98
[code=php:0]
<?
$query = "SELECT ('last_login','mot') FROM users WHERE username='$username'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if(time()-$row['last_login'] > 60*60*24)
{
$days = floor( (time()-$row['last_login']) / (60*60*24) );
$mot = $row['points'] + $days*100;
$timestamp = $row['last_login'] + $days*60*60*24;
mysql_query("UPDATE users SET mot='$mot',last_login='$timestamp' WHERE username='$username'");
}
?>
[/code]
Link to comment
Share on other sites

it isnt posting the new last_login time into it........plus ive now changed it to a datetime field because it works the same but i also know how to make that gmt time :P

[code=php:0]
<?
$query = "SELECT last_login,mot FROM users WHERE username='$username'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($time-$row['last_login'] > 60*60*24)
{
$days = floor( ($time-$row['last_login']) / (60*60*24) );
$mot = $row['points'] + $days*100;
$timestamp = $row['last_login'] + $days*60*60*24;
mysql_query("UPDATE users SET mot='$mot',last_login='$time' WHERE username='$username'");//its here that there is a problem as it isnt putting in the information
}
?>
[/code]
Link to comment
Share on other sites

Why did you change time() to $time?

Here's how the code should be:
[code]<?php

$query = "SELECT last_login,mot FROM users WHERE username='$username'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$now=time();
if($now-$row['last_login'] > 60*60*24)
{
$days = floor( ($now-$row['last_login']) / (60*60*24) );
$points = $row['points'] + $days*100;
$timestamp = $row['last_login'] + $days*60*60*24;
mysql_query("UPDATE users SET mot='$mot',last_login='$timestamp' WHERE username='$username'");
}

?>[/code]

$timestamp is define this way- [b]$timestamp = $row['last_login'] + $days*60*60*24[/b] and not as the current timestamp. This is inorder not to loose part of days- If it was updated in this way, only if you haven't logged in for 24 hours you get the points.

Orio.
Link to comment
Share on other sites

i ut all your code in and set the database a day behind and it didnt do anything....
[code=php:0]
<?
$query = "SELECT last_login,mot FROM users WHERE username='$username'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$now=time();
if($now-$row['last_login'] > 60*60*24)
{
$days = floor( ($now-$row['last_login']) / (60*60*24) );
$points = $row['points'] + $days*100;
$timestamp = $row['last_login'] + $days*60*60*24;
mysql_query("UPDATE users SET mot='$mot',last_login='$timestamp' WHERE username='$username'");
}

?>
[/code]
there must be a reason why it isnt posting the new timestamp in......should $timestamp not be $now??
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.