#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Paris, France
    Posts
    15
    Rep Power
    0

    Running Total help


    Happy Monday everyone,

    I have a loop that runs a formula for the last 3 years of a certain id and am struggling to get a 4th column that gives me a sum of those formulas.

    The formula is run on several different factors and I assign a point value depending on where they fall (over .9 gets 5, between .85 and .9 gets 4, and so on..)

    PHP Code:
    while($row mysql_fetch_assoc$result ))  
            { 
                
    $arby 1.25*$row['Salary'];
                
    $aav 0.75*$arby;
                if (
    $aav 300000)
                {
                    
    $aav 300000;
                }
                if (
    $row['Current'] != $lastteam)
                {
                    
    $lastteam $row['Current'];
                    echo 
    '</tr><tr class="odd"><td colspan="7" style="height:8px"></td></tr><tr class="header"><td>'.$row['team'].'</td>';
                    echo 
    '<td>Arby #</td> <td>Min. AAV</td> <td>2024</td> <td>2023</td> <td>2022</td> <td>TOT</td>';
                    
    $i=0;
                }
                if (
    == ($i++ & 1))
                {
                    
    $sClass 'odd';
                }
                else
                {
                    
    $sClass 'even';
                } 
                if (
    $row['PNums'] != $lastpnum
                { 
                    
    $lastpnum $row['PNums']; 
                    
    $lastplayer $row['Player'];
                    echo 
    '</tr><tr class="'.$sClass.'"><td><a href="player.php?PNum='.$row['PNums'].'" class="link">'.$lastplayer.'</a>, '.$row['Current'].'</td>';
                    echo 
    '<td>$ '.number_format($arby).' </td>'
                    echo 
    '<td>$ '.number_format($aav).' </td>'
                }
                
    $arbytot = ($ops $runs $rbi $hr $sb $row['GGs'] + $row['AS']);
                
    printf('
                <td>%0.1f</td>'
                
    PHP_EOL$arbytot); 
            } 
    *for brevity notice that I've omitted where I define what $ops, $runs, $rbi, etc. are, but they're in the code.

    So this gives me what I want almost:
    Player name, Team - $ Salary - $ Salary - Year1 pts - Year2 pts - Year3 pts

    The last thing I want is for this to give me a final "Total" column that adds up Year1pts, Year2pts and Year3pts

    Is there a simpler way to get what I'm looking for?

    Thanks in advance!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    First and foremost, stop using the deprecated MySQL extensions. Switch to PDO.
    Second, I'm not sure why this is a problem for you. Perhaps I am missing something. Why can't you initialize a total variable to zero before the loop then just add to it (+=) on each pass?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Paris, France
    Posts
    15
    Rep Power
    0
    Originally Posted by gw1500se
    First and foremost, stop using the deprecated MySQL extensions. Switch to PDO.
    Second, I'm not sure why this is a problem for you. Perhaps I am missing something. Why can't you initialize a total variable to zero before the loop then just add to it (+=) on each pass?
    1. because I'm very new to this stuff (didn't realize something was deprecated)
    2. this is what I was looking to do but wasn't sure how to go about doing it properly, everytime I tried something I'd do something wrong
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    Post your last attempt that didn't work and we can go from there but first rewrite your code to use PDO.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    724
    Rep Power
    7
    You seem to be running math items near the begining of your script just fine. Just a guess, but you looking something similar to...

    $pts[0] = 500;
    $pts[1] = 50;
    $pts[2] = 250;
    $total = array_sum($pts);
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Paris, France
    Posts
    15
    Rep Power
    0
    I actually figured it out, the (+=) was exactly what I was looking for!

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo