我是php和mysql的新手。我希望我在下面提供了足够的信息。
表名捐赠
|========================================================================|
| username | game | donation | month | year | time_stamp | id |
|========================================================================|
| TheSquatch | DayZ | 5 | June | 2015 | 2015-06-11 00:17:46 | 1 |
| TheMusic | DayZ | 20 | July | 2015 | 2015-07-10 03:20:46 | 2 |
| Sasquatch | DayZ | 35 | July | 2015 | 2015-07-10 03:26:04 | 3 |
|========================================================================|
这是我现在的剧本。
$result = mysqli_query($con,"SELECT username, game, donation, month, year, time_stamp, id FROM donations GROUP BY year, month ORDER BY id");
while($row = mysqli_fetch_array($result)) {
echo "<center><b>" . ($row['3']) . " " . ($row['4']) . "</b></center>";
echo str_repeat("<center>" . ($row['0']) . " $" . ($row['2']) . " (" . ($row['1']) . ")<br></center>",1
);
}
上面的代码目前给出了以下结果。
June 2015
TheSquatch $5 (DayZ)
July 2015
TheMusic $20 (DayZ)
我需要它给我以下的结果。(一个月内的所有捐款都不只是第一次。)
June 2015
TheSquatch $5 (DayZ)
July 2015
TheMusic $20 (DayZ)
Sasquatch $35 (DayZ)
如果我删除“按年、按月分组”,我会得到以下结果。
June 2015
TheSquatch $5 (DayZ)
July 2015
TheMusic $20 (DayZ)
July 2015
Sasquatch $35 (DayZ)
提前谢谢。
--------------------------------------------------
这是我在下面的结果中使用的最终代码。
$result = mysqli_query($con,"SELECT username, game, donation, month, year, time_stamp, id FROM donations ORDER BY id");
$old_row = 'blank';
while($row = mysqli_fetch_array($result)) {
if ($row['3'] . " " . $row['4'] != $old_row) {
$old_row = ($row['3'] . " " . $row['4']);
echo "<center><b>" . ($row['3']) . " " . ($row['4']) . "</b></center>";
}
echo str_repeat("<center>" . ($row['0']) . " $" . ($row['2']) . " (" . ($row['1']) . ")<br></center>",1
);
}
结果。
June 2015
TheSquatch $5 (DayZ)
July 2015
TheMusic $20 (DayZ)
Sasquatch $35 (DayZ)
--------------------------------------------------
最佳答案
$result = mysqli_query($con,"SELECT username, game, donation, month, year, time_stamp, id FROM donations ORDER BY id");
$old_row = 'blank';
while($row = mysqli_fetch_array($result)) {
if ($row['3'] . " " . $row['4'] != $old_row) {
$old_row = ($row['3'] . " " . $row['4']);
echo "<center><b>" . ($row['3']) . " " . ($row['4']) . "</b></center>";
}
echo str_repeat("<center>" . ($row['0']) . " $" . ($row['2']) . " (" . ($row['1']) . ")<br></center>",1
);
}