【问题标题】:Displaying data in tables depending on group根据组在表中显示数据
【发布时间】:2015-08-28 07:29:33
【问题描述】:

我有一个关于显示 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  ";

我知道我需要做的想法,但无法完成,因此我们将不胜感激。

【问题讨论】:

标签: php mysql html-table


【解决方案1】:

试试这个代码

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

【讨论】:

  • 工作,非常感谢,我有一个问题,那是由于一个团队有更多成员,最后一张桌子的对齐方式不正常,有没有简单的方法来解决它,我认为在表格底部添加边距会起作用,但没有运气? puu.sh/ilXFi/825675ffd0.png
  • 这个问题可能是因为每个团队有不同的 no 成员。如果你需要解决这个问题,你应该不知道团队的成员。如果您知道它通过添加空行来平衡每个团队表 (&lt;tr&gt;&lt;/tr&gt;)
【解决方案2】:

你可以做这个逻辑

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

【讨论】:

    【解决方案3】:

    尝试如下(请将表列名替换为你的,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>
    

    【讨论】:

    • 感谢您的贡献,我使用了另一种解决方案,但感谢您的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2018-06-11
    • 2020-09-06
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多