Echo content inside foreach only once

I try to repeat the contents inside foreach once. At the moment, when the form is filled in by the user, a message is displayed for each missed entry. If 35 entries are skipped, I will get 35 messages due to foreach. I want to avoid this and be able to display only one echo for the entire results page. How can i do this? I suppose I might have to do this outside my ancestor, but I have no idea how to get him out of the room.

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; } } } } 
+4
source share
7 answers

Assuming you have to maintain the structure of this object, you can simply have a boolean update if $course->aircraft == 1 , and then do the following:

 $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/>'; } 
+5
source

In this case, you can set a simple flag variable.

 $warningEmitted = false; 

Then in your loop before issuing a warning:

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

A better option would probably be to set your message as a variable and then repeat the variable after the foreach completes.

 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; } 
+1
source

Outside the loop, suppose $count=1;

Inside the loop, you can put an if statement.

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

Hope this helps.

+1
source

Just use the boolean variable you originally set to false and set it to true in the loop if you get a match.

You can then check the boolean after the loop completes to decide whether to display the message or not.

0
source

Create an additional variable in which you will store information about whether the message was already displayed or not. When you display it, set var to true.

0
source

Assuming I understand you correctly, I think you want to use "break" to stop the loop as soon as the problem is discovered.

 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; } } } 

Above, I also translated β€œif registered” conditional to be outside the loop (therefore, he only checked once).

Something to consider:

A more convenient approach is to add each error to the array - instead of using an echo and a break - and then skip this array of errors at the end, showing with additional error information so that they can be fixed once by the end user (depending on how your form works of course).

0
source

Source: https://habr.com/ru/post/1483886/


All Articles