简体   繁体   English

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

[英]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.

相关问题 php生成表的第一列没有标题 - No header of 1st column of php generated table 按字母顺序在mysql colum中添加第二个ID,其中第一列是酒店的ID,第二列是所添加软件包的ID - add second id in mysql colum alphabetically where 1st column is the id for hotel and second column is the id of the packages added PHP,MYSQL 仅获取结果数组的第一行和第二行 - PHP, MYSQL get 1st and second row only of result array PHP-Mysql:从第一行和第二行输出的 sql 中的 PHP 回显值 - PHP-Mysql : Php echo values from sql out put from 1st and second row MySQL:按第二列排序——如果不为空——同时保持第一列顺序 - MySQL: Order by 2nd column--if not null--while maintaining 1st column order 仅显示列中的第一个数据 - display 1st data only from column 如何格式化MySQL输出中的第一行与PHP中的所有后续行不同? - How to format 1st row in MySQL output differently from all following rows in PHP? 是否可以使用PHP PDO从第一张表中的列从第二张表中获取数据? - Is it possible to get data from the 2nd table using column in 1st table using PHP PDO? php,检查结果是否在mysql结果的第二列中 - php, check if a result is in second column of mysql result 登录表单在实时服务器上的第一次尝试失败,但在第二或第三次尝试时有效 - login form fails on 1st attempt on live server but works on second or third
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM