【问题标题】:Outputting mysql data to aggregated HTML table using PHP使用 PHP 将 mysql 数据输出到聚合的 HTML 表
【发布时间】:2022-01-17 05:01:24
【问题描述】:

我使用 JOIN 从各种表中选择数据,如下所示。

我希望使用 PHP 以下面的格式将其输出到 HTML 表(需要为每个不同的 game_id 创建这些表中的 1 个):

我有点迷茫,因为在我查找的所有SQL/PHP/HTML 表示例中,我还没有找到一个示例,它不仅仅是将 SQL 查询的输出直接转换为 HTML 表而不对其进行操作。例如https://www.w3schools.com/Php/php_mysql_select.asp

有没有人能指出我实现这一目标的最佳方法的正确方向?我不确定是否应该尝试编写一个更好的 SQL 查询来更好地满足我的需要,或者尝试使用 PHP 来完成繁重的工作。

注意:任何球队都可以有 1 名或多名球员。

谢谢

【问题讨论】:

    标签: php html mysql sql web-development-server


    【解决方案1】:

    我认为这个问题在数据库中没有技术解决方案,但我有解决方案。你如何看待它之后的第一个语句,然后是第二个语句和之后的语句,以此类推

    我所说的一个例子

    1. 球员:奥利弗、马修、杰克逊
    2. scors : 100,850,400
    3. 目标:2,7,5
    4. 跌倒:3,4,6

    如果这不能满足解释,请解释原因

    【讨论】:

    • 但我不明白你建议如何实现?
    • 简单地说,当游戏中发生事件时,您在数据库中更新它。如果您需要帮助,请在 Linkedin 上告诉我。
    【解决方案2】:

    好吧,就 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_uswins_opponent

    这些更改中的任何一个都更加优雅和可靠(然后您也可以取消 PHP 中的 $teamWinColumn 后备)。

    为了准确推荐如何执行此操作,您必须显示 SELECT 查询以及该查询中使用的任何表的 CREATE TABLE 语句。


    另外,为简洁起见,我在上面的代码中省略了&lt;table&gt;-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'] )
                    );
                }
    
    
        
    

    【讨论】:

    • 谢谢,我试试这个。为了澄清您关于回合胜利列的观点,这些列实际上属于另一个描述单个游戏而不是玩家统计数据的表 - 我在此查询中使用了 JOIN 来显示它,因此它不像看起来那么多余。
    • 这与冗余无关,更多的是使用数据(即“BLUE”和“RED”团队名称)作为列名。我不确定它是否总是恰好是 2 个团队,甚至是这 2 个确切的团队名称;但是您的 team_name 列是 VARCHAR/TEXT 并且在技术上可以包含一百万个其他值(甚至包括列名中甚至不允许出现的字符)。
    • 我完全理解你的意思,但问题是:永远只有 2 个团队,在这种情况下永远不会改变。团队的名称也不相关。团队名称列实际上是一个 ENUM,但我也可以只使用布尔数据类型(也许这样更有意义)。
    • 很公平 :) 你把剩下的工作搞定了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多