John Guan
John Guan

Reputation: 744

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

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

Upvotes: 0

Views: 249

Answers (4)

Bere
Bere

Reputation: 1747

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

or Based on @eggyal's query make a this modification and

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

Then you can find total number of names in a team in each row.

Upvotes: 0

Jakub Kania
Jakub Kania

Reputation: 16487

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

Upvotes: 3

Ruben Serrate
Ruben Serrate

Reputation: 2783

I'm not sure if I understand what you want but this could work:

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

Upvotes: 0

eggyal
eggyal

Reputation: 125865

You could get something similar directly from MySQL using the WITH ROLLUP modifier to its GROUP BY clause:

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

See it on sqlfiddle.

Rendering the results in the desired format would then simply be a case of switching on whether the Name column is NULL.

Upvotes: 1

Related Questions