捐款按年份和月份一起显示

时间:2015-07-10 10:44:02

标签: php mysql

我是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)

如果我删除“GROUP BY year,month”,我会得到以下结果。

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)

-------------------------------------------- ------

1 个答案:

答案 0 :(得分:1)

     $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
    );
    }