【问题标题】:Echo content inside foreach only once仅在 foreach 中回显内容一次
【发布时间】:2014-02-08 01:46:01
【问题描述】:

我试图在 foreach 中回显内容一次。目前,当用户填写表单时,会针对跳过的每条记录显示该消息。如果跳过了 35 条记录,我将收到 35 条消息,因为 foreach。我想避免这种情况,并且能够为整个结果页面仅显示一个回显。我怎样才能做到这一点?我想我可能必须在 foreach 之外执行此操作,但我不知道如何将其从 foreach 中取出。

foreach($allcourses as $course)
{
    if(Auth::LoggedIn())
    {
       if(Auth::$userinfo->rank == 'Student')
       {
           if($course->aircraft == '1')
           {
               echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
               continue; 
           }
           if($course->aircraft == '2')
           {
               echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
               continue; 
           }
        }
    }
}

【问题讨论】:

    标签: php


    【解决方案1】:

    假设您必须维护该对象的结构,如果 $course-&gt;aircraft == 1 然后相应地回显,则可以只进行布尔更新:

    $found = false;
    foreach($allcourses as $course)
    {
        if(Auth::LoggedIn())
        {
           if(Auth::$userinfo->rank == 'Student')
           {
    
               if($course->aircraft == '1')
               {
                   $found = true;
               }
            }
        }
    }
    if($found)
    {
        echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
    }
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您可以设置一个简单的标志变量。

      $warningEmitted = false;
      

      然后,在发出警告之前的循环中:

      if(!$warningEmitted) {
          // echo warning here. 
          $warningEmitted = true;
      }
      

      【讨论】:

        【解决方案3】:

        最好的选择可能是将您的消息设置为变量,然后在 foreach 完成后回显该变量。

        foreach($allcourses as $course)
        {
            if(Auth::LoggedIn())
            {
                if(Auth::$userinfo->rank == 'Student')
                {
                    if($course->aircraft == '1')
                    {
                        $message = '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
                        continue; 
                    }
                }
            }
        }
        if(isset($message))
        {
            echo $message;
        }
        

        【讨论】:

        • 出于某种原因,这不会返回任何记录 :( 值得注意的是,我有几个 if($course-&gt;aircraft == '1')。我已经更新了主要消息以向您展示我的意思。
        • 如果 $message 变量从未设置(即条件不评估为真),那么它根本不会回显。您的测试中可能就是这种情况,除非您在 foreach 中取消设置 $message 变量。
        【解决方案4】:

        在循环外假设$count=1;

        在循环内部,你可以放一个 if 语句。

        if($count==1) { $count++; echo "Whatever";}
        

        希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          只需使用最初设置为 false 的布尔变量,如果匹配,则在循环中将其设置为 true。

          然后你可以在循环结束后检查布尔值来决定是否需要显示消息。

          【讨论】:

            【解决方案6】:

            创建附加变量,无论消息是否已显示,您都将在其中存储信息。当你显示它时,将 var 设置为 true。

            【讨论】:

              【解决方案7】:

              假设我对您的理解正确,我认为您想在发现问题后立即使用“break”停止循环。

              if (Auth::LoggedIn() && Auth::$userinfo->rank == 'Student') {
                  foreach ($allcourses as $course) {
                      if ($course->aircraft == '1') {
                          echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
                          break; 
                      }
                      if ($course->aircraft == '2') {
                          echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
                          break; 
                      }
                  }
              }
              

              上面我还将“如果登录”条件移到了循环之外(所以它只检查一次)。

              需要考虑的事项:

              一种更加用户友好的方法可能是将每个错误添加到一个数组中 - 而不是使用 echo 和 breakout - 然后在最后循环遍历该错误数组,显示有关错误的更多信息,以便可以在最终用户一次(当然,取决于您的表单的工作方式)。

              【讨论】:

              • 使用break 不会阻止结果显示吗?在 ifs 下方,对于返回的每个 $course,我都有一个表格行。
              • Break 停止当前循环(并在循环后继续)。如果您在循环中也有未在此处公开的代码,那么您可能会从不同的解决方案中受益 - 不过我无法猜测,您需要提供更多信息。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-10-30
              • 2012-08-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多