在foreach循环中只输出一次内容

4
我正在尝试在foreach循环中仅输出一次内容。目前,当用户填写表单时,由于foreach的缘故,每个被跳过的记录都会显示消息。如果有35条被跳过的记录,我将会收到35条消息。我想避免这种情况,并能够在整个结果页面上仅显示一个echo。如何做到这一点呢?我想我可能需要在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; 
           }
        }
    }
}
7个回答

6
假设您必须保持该对象的结构,您可以只需设置一个布尔值更新,如果$course->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/>';
}

3
您可以在这种情况下设置一个简单的标志变量。
$warningEmitted = false;

然后,在发出警告之前的循环中:
if(!$warningEmitted) {
    // echo warning here. 
    $warningEmitted = true;
}

1
最好的选择可能是将您的消息设置为变量,然后在foreach完成后echo该变量。
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->aircraft == '1')。我已经更新了主要信息以向您展示我的意思。 - user2442178
如果 $message 变量从未设置(即条件不为真),则根本不会回显。这可能是您的测试情况,除非您在 foreach 中取消设置 $message 变量。 - Jason

1
在循环外部假设$count=1; 在循环内部,您可以放置一个if语句。
if($count==1) { $count++; echo "Whatever";}

希望这能帮到您。

0

假设我理解你的意思,我认为你想使用“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并退出 - 然后在最后循环遍历该错误数组,显示有关错误的更多信息,以便最终用户可以一次性进行更正(当然,这取决于您的表单如何工作)。


使用 break 不会停止结果的显示吗?在 if 语句下面,我为每个返回的 $course 添加了一个表行。 - user2442178
Break会停止当前循环(并在循环后继续执行)。如果您在循环中有未披露的代码,那么您可能需要另一种解决方案 - 我无法猜测,您需要提供更多信息。 - Iain Collins

0
只需使用一个布尔变量,初始设置为false,在循环中如果找到匹配项,则将其设置为true。
然后在循环结束后检查该布尔变量,以决定是否需要显示消息。

0

创建一个额外的变量,用于存储消息是否已经显示的信息。当您显示它时,请将变量设置为true。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接