Need help with update query in a loop.
Posted: Mon Jan 19, 2009 7:27 pm
Hi, Hope someone can see what I'm doing wrong with my loop, and update query.
What I'm trying to do is, in my game if a person uses this weapon it will randomly select other users.
For each person that it kills it gives the player 8 exp points. The 'if' in the loop, is suppose to check for each attack, if $bombexp+$player->exp is less then the players maxexp. It adds to $bombexp to the players exp.
If $bombexp +$player->exp would exceed the maxexp, it would increase their level and then go from there over and over until it reaches the end of the loop.
The problem I'm seeing is, if the query is ran I don't think it's checking for each row. What I mean is, if $bomb+$player->exp is less then max exp for the first row of the select query, it doesn't seem to check the rest of the rows. So a player could end up with a exp greater then maxexp.
Then the next time I run it, since the first row would now have a exp greater then maxexp, it then updates the level but then it assumes that each row is exceeds the maxexp, and increases the level by 1 for each row in the query. So the player ends up gaining 20 levels.
So I must just have my if statement in the wrong place right?
Here is what I have so far.
What I'm trying to do is, in my game if a person uses this weapon it will randomly select other users.
For each person that it kills it gives the player 8 exp points. The 'if' in the loop, is suppose to check for each attack, if $bombexp+$player->exp is less then the players maxexp. It adds to $bombexp to the players exp.
If $bombexp +$player->exp would exceed the maxexp, it would increase their level and then go from there over and over until it reaches the end of the loop.
The problem I'm seeing is, if the query is ran I don't think it's checking for each row. What I mean is, if $bomb+$player->exp is less then max exp for the first row of the select query, it doesn't seem to check the rest of the rows. So a player could end up with a exp greater then maxexp.
Then the next time I run it, since the first row would now have a exp greater then maxexp, it then updates the level but then it assumes that each row is exceeds the maxexp, and increases the level by 1 for each row in the query. So the player ends up gaining 20 levels.
So I must just have my if statement in the wrong place right?
Here is what I have so far.
Code: Select all
$bombexp = 8;
$newexp = $bombexp + $player->exp - $player->maxexp;
$selectplayers = $db->execute("select p.id, p.username from players as p
left join medical_ward as m on ( m.player_id = p.id )
where p.City_ID=? and m.player_id is null and p.id !=?
ORDER BY RAND() Limit 20", array($player->City_ID, $player->id));
while ($selectplayers1 = $selectplayers->fetchrow())
{
if ($bombexp + $player->exp < $player->maxexp)
{
$queryupdate = $db->execute("UPDATE `players` set `exp`=`exp`+? where `id`=?",array($bombexp,$player->id));
}
else //player has gained a level! =)
{
$query = $db->execute("UPDATE `players` set `level`=`level`+?, `maxexp`=?, `maxhp`=?, `exp`=?, `kills`=`kills`+?, `hp`=?, `energy`=?, `maxenergy`=?, `Max_Intent`=`Max_Intent`+? where `id`=?", array(1, ($player->level+1) * 70 - 20, $player->maxhp + 30, $newexp, 1, 0, 0, $player->maxenergy + 2, 1, $player->id));
}
}