根据组显示表中的数据

时间:2015-06-12 10:10:19

标签: php mysql html-table

我有一个关于显示PHP表格的问题应该是直截了当但我现在无法理解它,所以任何帮助都会受到赞赏,基本上我想做的是展示一个团队的玩家表,但显示多个用户表,并在其上方显示其团队名称。

我目前拥有的内容: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  ";

我知道我需要做的想法,但无法完成,所以任何帮助都会受到赞赏。

3 个答案:

答案 0 :(得分:1)

试试此代码

div:not(.ZoomBar):not(.row)

SQL查询更改如下

    <?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>

答案 1 :(得分:0)

你可以做这个逻辑     

$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>";
}

答案 2 :(得分:0)

请尝试以下操作(请将表列名称替换为您的,将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>