dongzhang4301 2019-06-02 11:27
浏览 107
已采纳

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

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!

  • 写回答

1条回答 默认 最新

  • drl2051 2019-06-02 11:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站