2015-03-19 132 views
0

enter image description herePHP MYSQL:第一欄作爲標題,第二和第三列行

我如何做列,teamname,作爲我行的頭。

1ShotGG沙塵暴2:

summonername2 
summoner 1 
summoner 2 
summoner 3 
summoner 4 

組#5

Summoner 1 
Summoner 1 

下面是我在代碼例如,我想使用PHP想輸出該結果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++; 
     } 

回答

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/>"; 
    } 
} 
+0

如果我想調用$ summoner循環中的召喚者ID,該怎麼辦?如何才能做到這一點? – 2015-03-20 01:50:22

+0

@PlusOne你只需稍微調整一下,而不是隻在收集'while'循環中只推送'summoner name',你可以直接推送整個'$ row',然後你可以自己分組行數組而不是名字,然後在'foreach'裏面而不是'$ summoner',現在它會像'echo $ summoner ['id']'等等 – Ghost 2015-03-20 02:58:56

+0

對不起,是否將團隊ID(與團隊名稱在同一行)包含在第一個循環中?我嘗試了各種方法,並且我得到了很多無效的結果。 – 2015-03-20 06:55:53

0

要打印每行的標題。您可能需要跟蹤當前和最後一排,看看球隊是否從最後一排變爲當前,並且只打印該車的頭部。 P

相關問題