Need help with If/ELSE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Need help with If/ELSE

Post 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))" );
 
        }
    }
 
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Need help with If/ELSE

Post by chopsmith »

Can you post the class you instantiated to get the $db object?
swellular
Forum Newbie
Posts: 3
Joined: Thu Nov 13, 2008 9:05 am

Re: Need help with If/ELSE

Post 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
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help with If/ELSE

Post 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.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Need help with If/ELSE

Post 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.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Need help with If/ELSE

Post 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.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Need help with If/ELSE

Post 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
Post Reply