PHP MySQL - 在循环中间添加一行


PHP MySQL - adding a row in the middle of a loop

我想知道是否可以在 PHP 的循环中间添加一行,而不必使用 WHERE 'team' = 'leader_name' 多次运行查询。假设我正在使用while ($row = mysqli_fetch_array($results))来显示我的结果。在查询中,我将每个团队拉入 1 个查询。所以我有 5 个团队,我想在每个团队之间有一个分隔符。可能吗?

当前结果:

Team Leader    |    Name
---------------+---------------------
 Team1         |     Name1
 Team1         |     Name2
 Team2         |     Name3
 Team3         |     Name4
 Team3         |     Name5
 Team3         |     Name6
 Team4         |     Name7
 Team4         |     Name8
 Team5         |     Name9
 Team5         |     Name10
 Team5         |     Name11
 Team5         |     Name12

预期成果:

Team Leader    |    Name
======================================
 Team1         |     Name1
 Team1         |     Name2
-------------------------------------
 Totals:       |     2
-------------------------------------
 Team2         |     Name3
-------------------------------------
 Totals:       |     1
-------------------------------------
 Team3         |     Name4
 Team3         |     Name5
 Team3         |     Name6
-------------------------------------
 Totals:       |     3
-------------------------------------
 Team4         |     Name7
 Team4         |     Name8
-------------------------------------
 Totals:       |     2
-------------------------------------
 Team5         |     Name9
 Team5         |     Name10
 Team5         |     Name11
 Team5         |     Name12
-------------------------------------
 Totals:       |     4
-------------------------------------

编辑

假设我使用

$data = mysqli_query ("Select * from tbl_name");
while ($row = mysqli_fetch_array($data)) {
    echo "<tr>";
        echo "<td>".$row['Team']."</td>";
        echo "<td>".$row['Name']."</td>";
    echo "</tr>";
}

您将如何实现以下示例

writeheader();
$team = $data[0]['team'];
$count = 0;
foreach($data as $row){
 $count += 1;
 //if the current row's team is different than previous than add seperator
 if($row['team'] != $team){
   $team = $row['team'];
   writeseperator();
   writetotal($count);
   $count = 0;
 }
 writerow();
}

我应该把它放在哪里?我有点困惑

当然有可能。不过你没有提供你的代码,所以我会给你伪代码。

writeheader();
$team = $data[0]['team'];
$count = 0;
foreach($data as $row){
 $count += 1;
 //if the current row's team is different than previous than add seperator
 if($row['team'] != $team){
   $team = $row['team'];
   writeseperator();
   writetotal($count);
   $count = 0;
 }
 writerow();
}
你可以

直接从MySQL获得类似的东西,使用WITH ROLLUP修饰符到它的GROUP BY子句:

SELECT   Team_Leader, Name, COUNT(*)
FROM     my_table
GROUP BY Team_Leader, Name WITH ROLLUP

在sqlfiddle上看到它。

然后,以所需的格式呈现结果只需打开Name列是否NULL

我不确定我是否理解你想要什么,但这可能会起作用:

$team="";
while ($row = mysqli_fetch_array($results)) {
    if($team!=$row["leader_name"]) {
        if(!empty($team)) {
            //echo your separator or whatever here
        }
        $team=$row["leader_name"];  
        $team_count=1;
    } else {
        $team_count++;
    }
}
$a= array(array("Team1","Name1"),array("Team1","Name2"),array("Team2","Name3"),array("Team3","Name4"),array("Team3","Name5"),array("Team3","Name6"),array("Team4","Name7"),array("Team4","Name8"),array("Team5","Name9"),array("Team5","Name10"),array("Team5","Name11"),array("Team5","Name12"));
$columns = array_unique(array_column($a, 0)); //get the 'TeamX' part and select unique ones
$count = 0;
foreach($columns as $column){ //for each unique team
   foreach($a as $row){
       if($row[0] == $column){ //search for a matching column and display it.
           //echo team and name
           $count++;  //keep track of items quantity
         }
       }      
   echo "-------------------------------------<br/>";
   echo "Totals: $count <br/>";
   echo "-------------------------------------";
   $count = 0; //reset count for the next team
}

或 根据@eggyal的查询进行此修改,然后

$query = "SELECT   a.Team_Leader, a.Name, cnt
FROM     my_table a 
left join (select Team_Leader, count(Name) as cnt from my_table group by Team_Leader)
b on a.Team_Leader = b.Team_Leader";

然后,您可以在每行中找到团队中的名字总数。