在嵌套循环内计数并在第二个嵌套循环之前使用值

So I have a nested loop due to getting data from two different sources. I want to count how many times it goes through the second loop, and use that value in the first loop.

I have 2 entries in the RaidFacade And 10 entries in "GetRaidProgression"

$raid_facade = new RaidFacade();
$raids = $raid_facade->getAll();
unset($raid_facade);
<div class='col-lg-4' id='toggleraid'>
  <div class='topbar'>Raid Progress</div>
  <?php
    $boss_count_alive = 0;
    $boss_count_killed = 0;
    foreach ($raids as $raid)
    {
      $raid_name = $raid->getName();
      echo "<div class='raid'>";
      echo "<div class='name'>";
      echo "<a class='collapsed' data-toggle='collapse' data-target='#raid".$raid_name."' aria-expanded='false' aria-controls='raid".$raid_name."'>";
      echo $raid_name;
      echo "</a>";
      echo "</div>";
      echo "<div class='prog'>".$boss_count_killed."<mark>/</mark>".$boss_count_alive."</div>";
      echo "<div class='bar'>";
      echo "<div class='color' style='width: 60%'></div>";
      echo "</div>";
      echo "<div class='gradient'></div>";
      echo "<img src='img/layout/raid/zul_gurub.jpg'>";
      echo "</div>";
      echo "<div id='raid".$raid_name."' class='collapse raidCollapse' data-parent='#toggleraid'>";
      foreach ($raid->getRaidProgression() as $boss)
      {
        $boss_count_alive++;
    $class = "fas fa-times fa-sm";
    $youtube = "";
        if ($boss->getStatus() == 1)
        {
          $class = "fas fa-check fa-sm";
      $boss_count_killed++;
        }
        echo "<div>";
        echo "<div><span><i class='".$class."'></i>".$boss->getBoss()."</span></div>";
    echo "</div>";
      }
      echo "</div>";
    }
  ?>
</div>

In the div class='prog' I would like to use the $boss_count_alive and $boss_count_killed values.

This is not happening, the first entry returns 0/0, the next one returns 3/10 (Which are my expected result for first entry)

To get a visual look:

enter image description here

Thanks in advance!

Your second loop will only run after you have echoed "<div class='prog'>".$boss_count_killed."<mark>/</mark>".$boss_count_alive."</div>";, which is why it's echoing 0/0 the first time.

If you want it to echo 3/10 the first time, you need to run your inner loop first to calculate the values, then echo. In your case it would be something like this:

foreach ($raids as $raid)
{
  $raid_name = $raid->getName();
  $bosses_html = "";
  foreach ($raid->getRaidProgression() as $boss)
  {
    $boss_count_alive++;
    $class = "fas fa-times fa-sm";
    $youtube = "";
    if ($boss->getStatus() == 1)
    {
      $class = "fas fa-check fa-sm";
      $boss_count_killed++;
    }
    $bosses_html .= "<div>";
    $bosses_html .= "<div><span><i class='".$class."'></i>".$boss->getBoss()."</span></div>";
    $bosses_html .= "</div>";
  }
  echo "<div class='raid'>";
  echo "<div class='name'>";
  echo "<a class='collapsed' data-toggle='collapse' data-target='#raid".$raid_name."' aria-expanded='false' aria-controls='raid".$raid_name."'>";
  echo $raid_name;
  echo "</a>";
  echo "</div>";
  echo "<div class='prog'>".$boss_count_killed."<mark>/</mark>".$boss_count_alive."</div>";
  echo "<div class='bar'>";
  echo "<div class='color' style='width: 60%'></div>";
  echo "</div>";
  echo "<div class='gradient'></div>";
  echo "<img src='img/layout/raid/zul_gurub.jpg'>";
  echo "</div>";
  echo "<div id='raid".$raid_name."' class='collapse raidCollapse' data-parent='#toggleraid'>";
  echo "</div>";
  echo $bosses_html;
}

In this example, the first set of echoes are moved below the inner loop. The html that would normally be echoed in the inner loop is stored in a variable to be echoed later at the end.