我已经看过另一个(很多已关闭)在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等等
我该怎么做?
答案 0 :(得分:0)
$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,逗号分开而不需要额外的数组。你正在寻找什么?