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

时间:2019-06-02 11:27:42

标签: php loops count nested

由于从两个不同的源获取数据,所以我有一个嵌套循环。 我想计算它经过第二个循环的次数,并在第一个循环中使用该值。

我在RaidFacade中有2个条目 在“ GetRaidProgression”中有10个条目

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

div class='prog'中,我想使用$boss_count_alive$boss_count_killed值。

这没有发生,第一个条目返回0/0,下一个返回3/10(这是我对第一个条目的预期结果)

要获得视觉效果:

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

您的第二个循环仅在您回显"<div class='prog'>".$boss_count_killed."<mark>/</mark>".$boss_count_alive."</div>";之后运行,这就是为什么它第一次回显0/0的原因。

如果您希望它第一次回声3/10,则需要先运行内部循环以计算值,然后回声。在您的情况下,将是这样的:

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

在此示例中,第一组回波被移动到内部循环下方。通常在内部循环中回显的html存储在变量中,以便稍后在末尾回显。