help with while loop (adding variables)

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

help with while loop (adding variables)

Post by cdoyle »

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

Code: Select all

 
while ($getaddons1 = $getaddons->fetchrow())
                            {
                                $supbonus += $getaddons1['corner_speed_increase']/100;
                                $speedbonus += $getaddons1['top_speed_increase']/100;
                                $accbonus += $getaddons1['acceleration_increase']/100;
 
                            }
 
Here is the rest of the code.

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

Re: help with while loop (adding variables)

Post by cdoyle »

I've been trying different things, and so far no luck.

I'm pretty certain that either I have this in the wrong spot, or this isn't the right way to do this.

Code: Select all

 
 while ($getaddons1 = $getaddons->fetchrow())
                             {
                                 $supbonus += $getaddons1['corner_speed_increase']/100;
                                 $speedbonus += $getaddons1['top_speed_increase']/100;
                                 $accbonus += $getaddons1['acceleration_increase']/100;
  
                            }
 
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: help with while loop (adding variables)

Post by Weiry »

Your code is looping through continuously adding the new values to the previous values, regardless of the player.

You may want to do something like this:

Code: Select all

$i = 0;
while ($getaddons1 = $getaddons->fetchrow())
{
      $carAddons[$i][0] = $getaddons1['corner_speed_increase']/100;
      $carAddons[$i][1] = $getaddons1['top_speed_increase']/100;
      $carAddons[$i][2] = $getaddons1['acceleration_increase']/100;
      $i++;
}
 
This way when you want to access each player's values, you need to lookup player '$i' inside $carAddons.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: help with while loop (adding variables)

Post by cdoyle »

Weiry wrote:Your code is looping through continuously adding the new values to the previous values, regardless of the player.

You may want to do something like this:

Code: Select all

$i = 0;
while ($getaddons1 = $getaddons->fetchrow())
{
      $carAddons[$i][0] = $getaddons1['corner_speed_increase']/100;
      $carAddons[$i][1] = $getaddons1['top_speed_increase']/100;
      $carAddons[$i][2] = $getaddons1['acceleration_increase']/100;
      $i++;
}
 
This way when you want to access each player's values, you need to lookup player '$i' inside $carAddons.
Thanks for replying, I'll give this a shot.
When you say 'I need to looup player $i inside $caraddons, not sure what you mean tho?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: help with while loop (adding variables)

Post by Weiry »

Well when you want to apply bonus' to player 7 for instance, you would look inside the $carAddon array for player 7 ($i = 7)

Code: Select all

//player 0 stats
$player0 = $carAddons[0];
//player 7 stats
$player7 = $carAddons[7];
And by looping through, i mean when you need to apply multiple player's stats

Code: Select all

 
$numPlayers = 7;
for($i=0; $i < $numPlayers; $i++){
     givePlayerStats($i, $carAddons[$i]); //givePlayerStats( playerID, playerBonusArray );
}
Where givePlayerStats() would be a function where you apply the stats for the particular player. (as an example)

Hope that makes more sense :D
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: help with while loop (adding variables)

Post by cdoyle »

Weiry wrote:Well when you want to apply bonus' to player 7 for instance, you would look inside the $carAddon array for player 7 ($i = 7)

Code: Select all

//player 0 stats
$carid0 = $carAddons[0];
//player 7 stats
$carid7 = $carAddons[7];
And by looping through, i mean when you need to apply multiple player's stats

Code: Select all

 
$numPlayers = 7;
for($i=0; $i < $numPlayers; $i++){
     givePlayerStats($i, $carAddons[$i]); //givePlayerStats( playerID, playerBonusArray );
}
Where givePlayerStats() would be a function where you apply the stats for the particular player. (as an example)

Hope that makes more sense
It's making a little sense, but I still have questions.
Would I hardcode this into a page?

Code: Select all

 
$carid0 = $carAddons[0];
//player 7 stats
$carid7 = $carAddons[7];
The reason I'm asking is because the races could have a minimum of 2 racers, and up to 8 I believe. Depending on what the creator chose. If the game picks up, I might allow them to have more racers per race. So want to make sure that is what you intended.

On this next part, $numplayers I can pull from the db, I think I have that OK.

Code: Select all

 
$numPlayers = 7;
for($i=0; $i < $numPlayers; $i++){
     givecarStats($i, $carAddons[$i]); //givecarstats( carID, carBonusArray );
}
I'm getting hung up on the function part though, what would it look like?

sorry for all the questions, and thank you for your help.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: help with while loop (adding variables)

Post by Weiry »

Well that part im not sure about either :mrgreen:

I'm not sure how you currently apply each player's upgrades.
My guess that:

Code: Select all

//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);
Is where you are reading your car values from for each player.

So i can really only say that, your function might be like this:

Code: Select all

function givePlayerStats($playerBonusArray, $getCarArr){
     $accel = $getCarArr['acceleration']+($getCarArr['acceleration']*$playerBonusArray[2]);
     $speed =  $getCarArr['max_speed']+($getCarArr['max_speed']*$playerBonusArray[1]);
     $susp = $getCarArr['max_corner_speed']+($getCarArr['max_corner_speed']*$playerBonusArray[0]);
 
     return array( "carID" => $getCarArr['car_id'], "accelleration" => $accel, "speed" => $speed, "suspension" => $susp);
}
 
$playerArray[] = givePlayerStats($carAddons[$i], $getcar->fetchrow());
 
Now im not sure how this will go, but hopefully its some sort of help.

Basically because your currently in a while loop already for all entries, we dont need a for loop for each player again.
Now because we are looping through each entry, we can store each car's information in a new array "$playerArray" which stores each player's stats and a car_id.

So you should just need to look for your car_id and apply the relative information.

Now i hope that helps, because i think i lost myself half way through when doing that XD
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: help with while loop (adding variables)

Post by cdoyle »

Thanks again for your help, I'm trying to understand how this would work but just not getting it.

I'll try and describe what I have so far, and see if this helps.

What I have currently is when the race starts, the first thing the script does is get all those entered in the race. This is pulled from the table race_entries
id
race_id
car_owned_id

Code: Select all

 
$getentries = $db->execute("SELECT re.race_id, re.car_owned_id FROM race_entries re
                                            INNER JOIN cars_owned co ON co.id=re.car_owned_id
                                            WHERE re.race_id=?", array($raceid));
 
What I did next was to get the actual information about the car entered into the race. (at second glance, I think I can also pull the car info from the previous query, but this is how it currently)
Table name cars_owned
id
player_id
car_id <<what car the player owns

Here is the getcar query.

Code: Select all

 
while($getentries1 = $getentries->fetchrow())
                    {
                        //get the car entered in the race for each player.
                        $getcar = $db->execute("SELECT c.car_name, co.efficiency, co.gas, co.plate, 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();
 
 
Next, what I did was run another query to get the addons for the car. These addons have 3 categories that increase performance (acceleration_increase, corner_speed_increase,top_speed_increase) these values are a percent ex 1, 2, 4 etc. A player could have no upgrades installed on the car, or have many.

table name: car_upgrades
id
acceleration_increase
corner_speed_increase
top_speed_increase

Code: Select all

 
 //get the upgrades for each car.
                        $getaddons = $db->execute("SELECT u.acceleration_increase, u.corner_speed_increase, u.top_speed_increase FROM car_upgrades u
                       INNER JOIN cars_upgradesinstalled cui ON cui.addon_id=u.id
                       WHERE cui.car_id=?", array($getcar1['carid']));
 
So I need to get the sum from each upgrade category.
for example
the player may have an exhaust installed that gives a 1% acceleration bonus, and also have an intake system installed that give a 2% bonus in acceleration.

So $accbonus for this car would be 3%
so to get the new acceleration for the car, I had something like this

Code: Select all

 
 //add bonus equipment
                        $acceleration = $getcar1['acceleration']+($getcar1['acceleration']*$accbonus);
 
I would need to do this for the maxspeed, and suspension categories as well.

Where I'm getting hungup with your solution, is how the function works.

I would keep my query that check for upgrades in my page right?

I don't understand how I assign
//player 0 stats
$carid0 = $carAddons[0];

also I'm not very experienced with functions, so I hope someone can help me out.
Where does $playerBonusArray and $getCarArr come from? From the upgrade check query?

Code: Select all

 
# function givePlayerStats($playerBonusArray, $getCarArr){
#      $accel = $getCarArr['acceleration']+($getCarArr['acceleration']*$playerBonusArray[2]);
#      $speed =  $getCarArr['max_speed']+($getCarArr['max_speed']*$playerBonusArray[1]);
#      $susp = $getCarArr['max_corner_speed']+($getCarArr['max_corner_speed']*$playerBonusArray[0]);
#  
#      return array( "carID" => $getCarArr['car_id'], "accelleration" => $accel, "speed" => $speed, "suspension" => $susp);
# }
#  
# $playerArray[] = givePlayerStats($carAddons[$i], $getcar->fetchrow());
 
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: help with while loop (adding variables)

Post by cdoyle »

I'm sorry to keep posting on this, but I'm still stuck.

I don't understand how to make this work.
if my query to get the addons stays on my page

Code: Select all

 
 $getaddons = $db->execute("SELECT u.acceleration_increase, u.corner_speed_increase, u.top_speed_increase FROM car_upgrades u
                       INNER JOIN cars_upgradesinstalled cui ON cui.addon_id=u.id
                       WHERE cui.car_id=?", array($getcar1['carid']));
 
Originally you said to try something like this, is this still correct?

Code: Select all

 
 $i = 0;
 while ($getaddons1 = $getaddons->fetchrow())
 {
       $carAddons[$i][0] = $getaddons1['corner_speed_increase']/100;
       $carAddons[$i][1] = $getaddons1['top_speed_increase']/100;
       $carAddons[$i][2] = $getaddons1['acceleration_increase']/100;
       $i++;
 }
 
I don't get how to take what it finds here (there could be more then 1 addon per car). Send it to the function, and then have the function add all of them together for each category. Then get that information back to my page, to use for the rest of the script?
Post Reply