Jump to content

Archived

This topic is now archived and is closed to further replies.

almightyegg

reset automatically

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??

Share this post


Link to post
Share on other sites
Do they always get 100 added every night?  If I register and don't log in for two weeks, when I log in next time, will I have 1400 points?

Regards
Huggie

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
we are not here to write your code for you. We are here to help debug your code that you yourself are working on.  If you want someone to write your code for you, please go post in the freelance forum (read the forum rules before doing so).

Share this post


Link to post
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 :)

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Try changing the three first lines to:
$query = "SELECT last_login,mot FROM users WHERE username='$username'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);

Orio.

Share this post


Link to post
Share on other sites
no errors...but there is no way of telling whether it works yet, unless i go change last login time in database? would that work??

Share this post


Link to post
Share on other sites
Yeah. Reduce 3601 from it and see if you get 100 points and the timestamp is being updated. Then run diffrent tests to check :)

Orio.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
That's not my error but yours =P
I posted in my original post last_login='$timestamp', and you changed it to $time from some reason :)

Orio.

Share this post


Link to post
Share on other sites
it didnt post with $timestamp either :P $time = date(info is in here) its an error in the code so its not posting...

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
im on an american server, therefore timestamp is at there time...i dont know how to change timestamp to gmt but i can change time()

Share this post


Link to post
Share on other sites
Whatever. I know what I did should work.
Any problems with the script?

Orio.

Share this post


Link to post
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??

Share this post


Link to post
Share on other sites
...and that's why we don't write code for people here.  That's why it's better left to freelancers who have access to the servers to make it work, for people who don't want to learn it on their own.

Share this post


Link to post
Share on other sites
Oh dear, what have I started.  I'm glad to see the method's been passed as acceptable... lol

Huggie

Share this post


Link to post
Share on other sites

×

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.