Page 1 of 1

Need help with If/ELSE

Posted: Sat Nov 15, 2008 11:24 am
by cdoyle
Hi,

I'm trying to make a cron file that runs each night, and I want it to work one way for one type of user, and runs another for another type of user.

What I'm trying to do is have it check the user status, if they are a donating user then it updates their account at a faster rate. If they are not a donating user, it updates at the standard rate.

Here is what I have now, I think I have my while in the wrong place?
What it does now is it updates all uses and resets their stats to the max limit. Which is not what I want it to do, it should only increase it by the set amounts below.
If I use just a fetchrow then it update all users at the donating rate. I think because it's only looking at the first row from the query. So this is why I think the while is in the wrong place????

Does this look close to being right?

Code: Select all

 
$currenttime = time();
    $checkstatus = $db->execute("Select `Mafia_Platty` FROM `players`");
 
    while ($checkstatus1 = $checkstatus->fetchrow()) {
           
        if ($checkstatus1['Mafia_Platty'] > $currenttime)  //currently a  donator
        {
            $query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" );
            $query = $db->execute("update `players` set energy = IF((energy + 15)>maxenergy, maxenergy, (energy + 15))" );
            $query = $db->execute("update `players` set fat = IF((fat - 5)< 0, 0, (fat - 5))" );
            $query = $db->execute("update `players` set Awake = IF((Awake + 2)>Max_Awake, Max_Awake, (Awake + 2))" );
            $query = $db->execute("update `players` set Intent = IF((Intent + 3)>Max_Intent, Max_Intent, (Intent + 3))" );
              
        }
        else //Not a donator
        {
            $query = $db->execute("update `players` set hp = IF((hp + 5)>maxhp, maxhp, (hp + 5))" );
            $query = $db->execute("update `players` set energy = IF((energy + 10)>maxenergy, maxenergy, (energy + 10))" );
            $query = $db->execute("update `players` set fat = IF((fat - 5)< 0, 0, (fat - 5))" );
            $query = $db->execute("update `players` set Awake = IF((Awake + 2)>Max_Awake, Max_Awake, (Awake + 2))" );
            $query = $db->execute("update `players` set Intent = IF((Intent + 2)>Max_Intent, Max_Intent, (Intent + 2))" );
 
        }
    }
 

Re: Need help with If/ELSE

Posted: Sat Nov 15, 2008 2:03 pm
by chopsmith
Can you post the class you instantiated to get the $db object?

Re: Need help with If/ELSE

Posted: Sat Nov 15, 2008 4:45 pm
by swellular
What it does now is it updates all uses and resets their stats to the max limit.
Naturally, you didn't tell it not to. :)
$query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" );
Each time you run that section of code, it will update EVERYBODY's record each time it goes through the while loop.

What you probably want to do is add a where clause to each of the update queries, so they end up something like this:

Code: Select all

 
$THIS_PLAYERS_PLAYER_ID = $checkstatus1['PLAYER_ID']; //or whatever the identifying field is
$query = $db->execute("update `players` set hp = if((hp + 10)>maxhp, maxhp, (hp + 10)) [b]WHERE PLAYER_ID={$THIS_PLAYERS_PLAYER_ID}[/b]" );
$query = $db->execute("update `players` set energy = IF((energy + 15)>maxenergy, maxenergy, (energy + 15)) [b]WHERE PLAYER_ID={$THIS_PLAYERS_PLAYER_ID}[/b]");
// ...
// yadda yadda yadda
 

Re: Need help with If/ELSE

Posted: Sat Nov 15, 2008 5:43 pm
by requinix
swellular wrote:What you probably want to do is add a where clause to each of the update queries, so they end up something like this:
Or, since it would do that for all the players anyways, keep the global UPDATE and just get rid of the loop.

Re: Need help with If/ELSE

Posted: Sun Nov 16, 2008 9:44 am
by cdoyle
tasairis wrote:
swellular wrote:What you probably want to do is add a where clause to each of the update queries, so they end up something like this:
Or, since it would do that for all the players anyways, keep the global UPDATE and just get rid of the loop.
When you say get rid of the loop, do you mean use a standard fetchrow?

So something like this?

Code: Select all

 
 $currenttime = time();
    $checkstatus = $db->execute("Select `Mafia_Platty` FROM `players`");
    $checkstatus1 = $checkstatus->fetchrow(); 
      
 
    if ($checkstatus1['Mafia_Platty'] > $currenttime)  //currently a  donator
    {
        $query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" );
        $query = $db->execute("update `players` set energy = IF((energy + 15)>maxenergy, maxenergy, (energy + 15))" );
        $query = $db->execute("update `players` set fat = IF((fat - 5)< 0, 0, (fat - 5))" );
        $query = $db->execute("update `players` set Awake = IF((Awake + 2)>Max_Awake, Max_Awake, (Awake + 2))" );
        $query = $db->execute("update `players` set Intent = IF((Intent + 3)>Max_Intent, Max_Intent, (Intent + 3))" );
 
    }
    else //Not a donator
    {
        $query = $db->execute("update `players` set hp = IF((hp + 5)>maxhp, maxhp, (hp + 5))" );
        $query = $db->execute("update `players` set energy = IF((energy + 10)>maxenergy, maxenergy, (energy + 10))" );
        $query = $db->execute("update `players` set fat = IF((fat - 5)< 0, 0, (fat - 5))" );
        $query = $db->execute("update `players` set Awake = IF((Awake + 2)>Max_Awake, Max_Awake, (Awake + 2))" );
        $query = $db->execute("update `players` set Intent = IF((Intent + 2)>Max_Intent, Max_Intent, (Intent + 2))" );
 
 
 
    }
 
This doesn't work, since I think it's only looking at the first row of the query, and since that player is not a donator, it updates everyone as a non donator.

Re: Need help with If/ELSE

Posted: Sun Nov 16, 2008 9:45 am
by cdoyle
swellular wrote:
What it does now is it updates all uses and resets their stats to the max limit.
Naturally, you didn't tell it not to. :)
$query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" );
Each time you run that section of code, it will update EVERYBODY's record each time it goes through the while loop.

What you probably want to do is add a where clause to each of the update queries, so they end up something like this:

Code: Select all

 
$THIS_PLAYERS_PLAYER_ID = $checkstatus1['PLAYER_ID']; //or whatever the identifying field is
$query = $db->execute("update `players` set hp = if((hp + 10)>maxhp, maxhp, (hp + 10)) [b]WHERE PLAYER_ID={$THIS_PLAYERS_PLAYER_ID}[/b]" );
$query = $db->execute("update `players` set energy = IF((energy + 15)>maxenergy, maxenergy, (energy + 15)) [b]WHERE PLAYER_ID={$THIS_PLAYERS_PLAYER_ID}[/b]");
// ...
// yadda yadda yadda
 
I'll give this a try and see if it works.

Re: Need help with If/ELSE

Posted: Mon Nov 17, 2008 9:29 am
by cdoyle
cdoyle wrote:
swellular wrote:
What it does now is it updates all uses and resets their stats to the max limit.
Naturally, you didn't tell it not to. :)
$query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" );
Each time you run that section of code, it will update EVERYBODY's record each time it goes through the while loop.

What you probably want to do is add a where clause to each of the update queries, so they end up something like this:

Code: Select all

 
$THIS_PLAYERS_PLAYER_ID = $checkstatus1['PLAYER_ID']; //or whatever the identifying field is
$query = $db->execute("update `players` set hp = if((hp + 10)>maxhp, maxhp, (hp + 10)) [b]WHERE PLAYER_ID={$THIS_PLAYERS_PLAYER_ID}[/b]" );
$query = $db->execute("update `players` set energy = IF((energy + 15)>maxenergy, maxenergy, (energy + 15)) [b]WHERE PLAYER_ID={$THIS_PLAYERS_PLAYER_ID}[/b]");
// ...
// yadda yadda yadda
 
I'll give this a try and see if it works.
I was able to get it to work by doing this,

Code: Select all

 
  //donating updates
        $query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10)) WHERE `donation_days` > $currenttime" );
 
  //normal users
        $query = $db->execute("update `players` set hp = IF((hp + 5)>maxhp, maxhp, (hp + 5)) WHERE `donation_days` <= $currenttime" );
 
seems to be working