[英]PHP MYSQL: 1st column as a header, second and third column as rows
How do I made the column, teamname, as a header for my rows.我如何将列团队名称作为行的标题。 For example, I would like to output this result using PHP:例如,我想使用 PHP 输出这个结果:
********* Sandstorm 2:** ********* 沙尘暴2:**
summonername2
summoner 1
summoner 2
summoner 3
summoner 4
Team #5团队 #5
Summoner 1
Summoner 1
Here is the code that I have in PHP.这是我在 PHP 中的代码。 So far, its outputting unexpected results.到目前为止,它输出了意想不到的结果。
$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++;
}
As an alternative, you could also use the team name
as your key for gathering all values inside grouping them.作为替代方案,您还可以使用team name
作为您的关键字,用于收集分组内的所有值。
After you have gathered them, then present them accordingly:收集它们后,然后相应地呈现它们:
$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/>";
}
}
You're printing the heading for each row.您正在打印每一行的标题。 You probably need to keep track of both the current and the last row, see if the team has changed from the last row to the current, and only print the head in that car.您可能需要跟踪当前和最后一行,查看团队是否从最后一行更改为当前,并且只打印该车的头部。 P磷
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.