Wednesday, 11 September 2013

Problems with logic of my code

Problems with logic of my code

I am creating a bitcoin faucet, and need a little help with making it
better. Basically what is happening is that if vistor A visits the website
for the first time, it try's to get some info (timestamp of last faucet
claim), but will fail, since there is no data in the db for the person.
Now if person B does this, there will be no error. I need help with coding
this so that i do not have to make a if/else statement, otherwise I will
have to copy and paste a HUGE chunk of code, and if I change the code in
the if statement, then i have to do the same on the other side... Here is
the code...
$ip = $_SERVER["REMOTE_ADDR"];
$chkquery = "SELECT * FROM faucet where ip='$ip' ORDER BY timestamp DESC
LIMIT 1;";
$chk = mysql_query($chkquery) or die($chkquery."<br/><br/>".mysql_error());
while($row = mysql_fetch_array($chk, MYSQL_ASSOC)){
$timestamp = $row['timestamp'];
}
date_default_timezone_set('America/Los_Angeles');
$timenow = time();
$chktime = strtotime($timestamp);
$time_diff = $timenow - $chktime;
if($chktime <= ($timenow - 1800)) {
//LOTS OF CODE
} else {
$address_error = "Sorry, you have to wait 30 minutes...";
}
The thing is that if a person visits the site for the FIRST time,
$timestamp will be none, since there is no info about him on the db (this
error: http://puu.sh/4p3PL.png)... I need to fix this without copying and
pasting the "LOTS OF CODE" part. Also i cant set $code to all of the code,
and then make an if statement and just place $echo, because I have a lot
of php in there with both " and '... I hope this makes sense, and thanks
for all the help!

No comments:

Post a Comment