好吧,就 SQL 查询而言,您已经完成了。
将最终的 html 输出塑造成您想要的样子,从那里一直到应用程序(即 PHP)。
第一步是使用 PHP 和 HTML 粗略地重新创建您在 image 1 - query output 上看到的内容。
我想您已经设法(或能够做到)达到这一点?
该代码可能类似于:
$records = $db->query('SELECT .....');
echo "<table>";
foreach ($records as $record) {
printf('<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>',
htmlentities($record['map_name']),
htmlentities($record['first_name']),
htmlentities($record['score'])
); // ( ...etc; only used 3 random columns here to illustrate)
}
echo "</table>";
一旦你完成了这项工作,你就可以根据自己的喜好塑造它。
首先,您需要对每场比赛以及每轮/每支球队的记录进行分组。
您可以通过将上面的代码替换为:
// Step 1 (Query the database for the raw data)
$records = $db->query('SELECT .....'); // same as before
// Step 2 (Organise the data in a way that suits your needs)
$games = []; // we'll order the data into this empty array
foreach ($records as $record) {
$thisGameID = (int) $record['game_id'];
$thisTeam = (string) $record['team'];
// If thisGameID didnt already exist in $games, we add it (as an empty array)
if (!isset($games[ $thisGameID ])) $games[ $thisGameID ] = [];
// If thisTeam doesnt already exist within there, we add that aswell (as an empty array)
if (!isset($games[ $thisGameID ][ $thisTeam ])) $games[ $thisGameID ][ $thisTeam ] = [];
// Now we can add this record to that
$games[ $thisGameID ][ $thisTeam ][] = $record;
}
现在您有一个单独的记录集,每个团队的每场比赛都分组。创建输出做类似的事情
// Step 3 (Build html output based on the organized data)
foreach ($games as $thisGameID => $game) { // Iterate the buffer, per match
echo "<table>";
foreach ($game as $teamIndex => $records) { // And iterate within that, per team
foreach ($records as $rowIndex => $record) {
$teamHeader = ''; // Empty placeholder for a header field (the '<td rowspan=X>...</td>' field)
if ($rowIndex === 0) { // If this is the first row for this match/team-combo
$roundRowCount = count($records); // How many rows are there for this match/team-combo
$teamWinColumn = $teamIndex . '_round_wins';
$teamHeader = sprintf(
'<td rowspan="%d">%s round wins %s</td>',
$roundRowCount,
htmlentities($record['team']),
isset($record[ $teamWinColumn ]) // We have to account for the wins-column perhaps not existing;
? (int) $record[ $teamWinColumn ] // like if the record has team='GREEN' and theres no `GREEN_round_wins` column
: 'UNKNOWN!' // <-- then we display this as fallback.
);
}
printf('<tr>
%s
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>',
$teamHeader,
htmlentities( $record['first_name'] ),
htmlentities( $record['score'] ),
htmlentities( $record['Goals'] ),
htmlentities( $record['Falls'] )
);
}
}
echo "</table>";
}
就我个人而言,我建议稍微改进一下 SQL 查询,这样你就没有两个名为 ..._round_wins 的列,而是:
- 其中一个:1 个
rounds_won 列,包含 此记录中的人所属团队的胜利
- 或者:2 列
wins_us 和 wins_opponent
这些更改中的任何一个都更加优雅和可靠(然后您也可以取消 PHP 中的 $teamWinColumn 后备)。
为了准确推荐如何执行此操作,您必须显示 SELECT 查询以及该查询中使用的任何表的 CREATE TABLE 语句。
另外,为简洁起见,我在上面的代码中省略了<table>-header。但是如果你想添加它,
改变:
foreach ($games as $thisGameID => $game) { // Iterate the buffer, per match
echo "<table>";
foreach ($game as $teamIndex => $records) { // And iterate within that, per team
foreach ($records as $rowIndex => $record) {
到
foreach ($games as $thisGameID => $game) { // Iterate the buffer, per match
$gameHeaderPrinted = false;
echo "<table>";
foreach ($game as $teamIndex => $records) { // And iterate within that, per team
foreach ($records as $rowIndex => $record) {
if (!$gameHeaderPrinted && ($gameHeaderPrinted = true)) {
printf('<tr><th colspan="%s">GAME-%s (some_date_here): %s</th></tr>',
5, // <--- should match the total amount of columns the table has
htmlentities( $record['game_id'] ),
htmlentities( $record['map_name'] )
);
}