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 个团队,我希望每个团队之间都有一个分隔符.可能吗?
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?
当前结果:
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>";
}
您将如何实现下面的示例
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
推荐答案
当然有可能.你没有提供你的代码,所以我会给你伪代码.
Of course it is possible. You didn't provide your code though so I'll give you pseudocode.
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();
}
这篇关于PHP MySQL - 在循环中间添加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!