help with while loop (adding variables)
Posted: Sun Dec 06, 2009 4:33 pm
Hi,
I need some help with a piece of code I'm writing.
What this is suppose to do, is give the player a bonus for the upgraded addons they've installed to their car.
So if player 1 has several addons that give an acceleration bonus, it suppose to add those together for that player and give a total ($accbonus)
but instead it's adding the bonus up for each player and then giving the next player the combined bonus. So the last player to join, get a combined $accbonus for all other players.
How do I add all the addons bonus for each part, per player and not give the next player to join a bonus?
I think it has to do with where I have this part coded, not sure if it being inside the $getentries1 loop is causing it to do this or not. If it is, not sure where else it should go it needs to get the stats for each car.
I'm using adodb and mySQL
Here is the rest of the code.
I need some help with a piece of code I'm writing.
What this is suppose to do, is give the player a bonus for the upgraded addons they've installed to their car.
So if player 1 has several addons that give an acceleration bonus, it suppose to add those together for that player and give a total ($accbonus)
but instead it's adding the bonus up for each player and then giving the next player the combined bonus. So the last player to join, get a combined $accbonus for all other players.
How do I add all the addons bonus for each part, per player and not give the next player to join a bonus?
I think it has to do with where I have this part coded, not sure if it being inside the $getentries1 loop is causing it to do this or not. If it is, not sure where else it should go it needs to get the stats for each car.
I'm using adodb and mySQL
Code: Select all
while ($getaddons1 = $getaddons->fetchrow())
{
$supbonus += $getaddons1['corner_speed_increase']/100;
$speedbonus += $getaddons1['top_speed_increase']/100;
$accbonus += $getaddons1['acceleration_increase']/100;
}
Code: Select all
while($getentries1 = $getentries->fetchrow())
{
$getcar = $db->execute("SELECT c.car_name, co.efficiency, co.gas, c.max_speed, c.acceleration, c.max_corner_speed, co.driving_skills, co.car_id, co.id as carid,
p.username, p.driving_hours, p.id FROM Cars c
INNER JOIN cars_owned co ON co.car_id=c.car_id
INNER JOIN players p ON p.id=co.player_id
WHERE co.id=?", array($getentries1['car_owned_id']));
$getcar1 = $getcar->fetchrow();
//get the upgrade parts and their bonus.
$getaddons = $db->execute("SELECT u.acceleration_increase, u.corner_speed_increase, u.top_speed_increase FROM car_upgrades u
LEFT JOIN cars_upgradesinstalled cui ON cui.addon_id=u.id
WHERE cui.car_id=?", array($getentries1['car_owned_id']));
while ($getaddons1 = $getaddons->fetchrow())
{
//add up the bonus per attribute.
$supbonus += $getaddons1['corner_speed_increase']/100;
$speedbonus += $getaddons1['top_speed_increase']/100;
$accbonus += $getaddons1['acceleration_increase']/100;
}
//add bonus equipment to total.
$acceleration = $getcar1['acceleration']+($getcar1['acceleration']*$accbonus);
$speed = $getcar1['max_speed']+($getcar1['max_speed']*$speedbonus);
$suspension = $getcar1['max_corner_speed']+($getcar1['max_corner_speed']*$accbonus);