根据组在表中显示数据

Displaying data in tables depending on group

我有一个关于显示 PHP table 的问题应该是直截了当的,但我现在无法理解它所以任何帮助将不胜感激,基本上是我想要做的是在 table 中显示一个玩家团队,但显示多个 table 用户并在其上方显示他们的团队名称。

我目前拥有的:http://puu.sh/ilUJp/4a6ae5e47b.png 我想要实现的目标:http://puu.sh/ilUJ8/7756033517.png

<div class="col-lg-6">
                <h3>Team Name Goes Here </h3>
   <?php              

echo "<table class='table table-striped'>";
echo "  <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    ";
while($row = mysqli_fetch_array($result)) {
    $teamName = $row['teamName'];
    $fName = $row['firstName'];
    $surName = $row['surName'];
    echo "
        <tbody>
      <tr>
        <td>$teamName</td>
        <td>$fName</td>
        <td>$surName</td>
      </tr>       
    </tbody>

     ";

} 

echo "</table>";
?>
            </div>

我的查询:

$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID  ";

我知道我需要做的想法但无法完成,所以任何帮助将不胜感激。

你可以做这个逻辑

$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
    $teamid = $row['teamid'];
    $teamname = $row['teamname'];

    $teammemberquery = "select all member in the  where team = $teamid sql query";
    echo "<table>";
    while ($r = mysqli_fetch_array($teammemberquery)) {
        $teamName = $r['teamName'];
        $fName = $r['firstName'];
        $surName = $r['surName'];
        echo "
        <tbody>
      <tr>
        <td>$teamName</td>
        <td>$fName</td>
        <td>$surName</td>
      </tr>       
    </tbody>
     ";
    }
    echo "</table>";
}

试试这个代码

    <?php $teemid=array();
    while($row = mysqli_fetch_array($result)) {
          if(!in_array($row['teamID'],$teemid)){
                 array_push($teemid,$row['teamID']);  
             if(!empty($teemid)){ ?>
                   </tbody>
                  </table>       
                </div>
           <?php } 

     ?>
       <div class="col-lg-6">
           <h3><?php echo $row['teamName']; ?></h3>
           <table class='table table-striped'>
              <thead>
                 <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Email</th>
                 </tr>

              </thead>
              <tbody>
             <?php } ?>
              <tr>
                  <td><?php echo $row['teamName']; ?></td>
                  <td><?php echo $row['firstName']; ?></td>
                  <td><?php echo $row['surName']; ?></td>
             </tr> 


    <?php } ?>
             </tbody>
           </table>       
       </div>

SQL查询更改如下

$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID  ORDER BY u.teamID";

尝试如下(请将 table 列名称替换为您的名称,并将 mysql 替换为 mysqli):

    <?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
    <?php              
        echo "<table>";

        $teamName = "";
        $i=0;
        while($row = mysql_fetch_array($result)) 
        {
            if($teamName == "" || $teamName != $row['team'])
            {
                if($i!=0)
                    echo "</table>";

                echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
                $teamName = $row['team'];
                $i=0;
            }
            $fName = $row['fname'];
            $surName = $row['lname'];
            $email = $row['email'];

            if($i==0)
            {
                echo "<table class='table table-striped'><tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Email</th>
                </tr>";
            }

            echo "<tr>
                <td>$fName</td>
                <td>$surName</td>
                <td>$email</td>
                  </tr>";

            $i++;
        } 
        echo "</table>";
    ?>
</div></body></html>