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.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘