我试图回忆一下foreach中的内容.此时,当用户填写表单时,将显示每个跳过的记录的消息.如果跳过35条记录,由于foreach,我将收到35条消息.我想避免这种情况,并且只能为整个结果页面显示一个回声.我怎样才能做到这一点?我想我可能不得不在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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
假设您必须维护该对象的结构,那么如果$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/>';
}
Run Code Online (Sandbox Code Playgroud)