PHP MYSQL:第一列作为标题,第二列和第三列作为行

时间:2015-03-19 01:39:21

标签: php mysql

enter image description here

如何将列teamname作为行的标题。例如,我想使用PHP输出此结果:

1ShotGG Sandstorm 2:

summonername2
summoner 1
summoner 2
summoner 3
summoner 4

第5队

Summoner 1
Summoner 1

这是我在PHP中的代码。到目前为止,它输出了意想不到的结果。

$query = "SELECT players.id, team.teamname, players.summonername 
        from team
        INNER JOIN players ON players.team = team.id
        WHERE team.tourneyid = {$id}";
        $result = mysqli_query($conn, $query);
        while($row = mysqli_fetch_assoc($result)) {
            if($i == 0) {
                $heading .= "<h3>". $row['teamname'] ."</h3><table class='table'><tr><th>Summonername</th><th>rank</th></tr>";
            }// team header
            $teams .= "{$heading}<tr><td>". $row['summonername'] ."</td><td>rank</td></tr>";
            $i++;
        }

2 个答案:

答案 0 :(得分:1)

作为替代方案,您还可以使用team name作为关键字来收集分组内的所有值。

收集完毕后,请相应地展示:

$query = "
    SELECT players.id, team.teamname, players.summonername 
        FROM team
        INNER JOIN players ON players.team = team.id
        WHERE team.tourneyid = {$id}
";

$teams = array();
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)) {
    $team_name = $row['teamname'];
    $summoner_name = $row['summonername'];
    $teams[$team_name][] = $summoner_name; // continually push same team names under a container
}

// then for presentation
foreach($teams as $team_name => $summoners) {
    echo "<h3>{$team_name}</h3><br/>";
    foreach($summoners as $summoner) {
        echo "<p>{$summoner}</p><br/>";
    }
}

答案 1 :(得分:0)

您正在为每行打印标题。您可能需要跟踪当前行和最后一行,查看团队是否已从最后一行更改为当前行,并且只打印该行中的头部。 P