I am wondering if it's possible to add a row in a middle of a loop in PHP without having to run the query multiple times using WHERE 'team' = 'leader_name' . Let's saying I am using while ($row = mysqli_fetch_array($results)) to display my results. In the query, I am pulling every team in 1 query. So I have 5 teams and I want to have a separator between each team. Is it possible?
Current results:
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
Expected results:
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
-------------------------------------
Edit
Let's say I use
$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>";
}
How would you implement the example below
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();
}
Where should I put it? I am kinda confused