繁体   English   中英

如何计算运行总额?

[英]How to calculate a running total?

我看了PHP中其他(很多已关闭)正在运行的总问题,但是都不满意

这是我的查询

<?php
$query = "SELECT SUM(game_total) AS totalgames, 
                 SUM(goals) AS totalgoals, 
                 FROM player 
                 WHERE year between 2006 and 2013 
                 AND month between 1 and 12 
                 GROUP BY 
                 year,
                 month between 1 and 3,
                 month between 4 and 6,
                 month between 7 and 9,
                 month between 10 and 12
                 ORDER BY year ASC, month ASC";
$rquery = mysql_query($query) or die(mysql_error());

$show2 = '';

    while ($row = mysql_fetch_array($rquery)){

                $games = $row['totalgames'];
                $goals = $row['totalgoals'];

                $gpg = $games == 0 ? 0 : number_format($goals/$games,2);
}
    ?>

如果我回显完成循环的查询结果,则会得到以下结果

+---------+----------+-----------+
|  Goals  |   Games  |   GPG     |
+---------+----------+-----------+
|  8      |   15     |   0.53    |
+---------+----------+-----------+
|  2      |   12     |   0.17    |
+---------+----------+-----------+
|  2      |   12     |   0.17    |
+---------+----------+-----------+
|  13     |   21     |   0.62    |
+---------+----------+-----------+

但是,我想按以下方式计算运行的GPG

Goals/Games=GPG
8/15=0.53
10/27=0.37 (8+2/15+12)=GPG
12/39=0.31 (10+2/27+12)=GPG
25/60=0.41 (12+13/39+21)=GPG

我想这样回应

0.53,0.37,0.31,0.41等

我该怎么做?

      $count = 0;
      while ($row = mysql_fetch_array($rquery)){

            $count++; 

            $games = (int)$row['totalgames'];
            $goals = (int)$row['totalgoals'];

            $totalGames = 0;
            $totalGoals = 0;

            $totalGames += $goalData['games'];
            $totalGoals += $goalData['goals'];

            echo $totalGoals/$totalGames;
            echo ($count === mysql_num_rows($rquery)-1) '' : ',';

}

我们去了,在迭代过程中运行总GPG,逗号分隔而没有额外的数组。 那是您要找的东西吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM