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

时间:2013-09-16 12:42:16

标签: php mysql

我想知道是否可以在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();
}

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

4 个答案:

答案 0 :(得分:3)

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

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();
}

答案 1 :(得分:1)

您可以使用WITH ROLLUP modifierGROUP BY子句直接从MySQL获得类似内容:

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

sqlfiddle上查看。

以所需格式呈现结果只是一种情况,即切换Name列是否为NULL

答案 2 :(得分:0)

我不确定我是否理解你的想法,但这可行:

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

答案 3 :(得分:0)

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

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