[英]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.