8 months ago

Loop inside a loop. Continue at the index+1 where the intern loop stop

Posted 8 months ago by RafaelMunoznl

This is a kind of twit togue, but I try to explain as good as I can.

I have a loop inside a loop as you could see here:

@for($i = 8; $i <= 17; $i++) // These are the hours. From 8:00 hour to 17:00 hour (German hour format)

    @foreach($employee->appointments as $appointment) // I take the appointment of each employee
        @if( $appointment->date->translatedFormat('D j M') != today()->translatedFormat('D j M')) 
            @continue //if the appointment is not from today, continue
            @if( $appointment->date->format('H') != $i )
                <h3> <a href="link_to_create_appointment_form">Add an appointment</a></h3>
                // insert a link to create a new apppointment
                // insert the Appointment

My problem is as follow:

  • After the first appointment is inserted (Appointment == today && now()), the foreach goes to next appointment, but start from the beginning.

  • At the end if I have an appointment at 9:00 and another at 10:00 I get the following:

8:00 - add an appointment
8:00 - add an appointment  // <---- the ouside loop started from the beginning of the day
9:00 - add an appointment

You could see an image in the following link: http://severus.es/wp-content/uploads/2019/11/termin-plan.png

The question is:

  • How could I force the following loop of the foreach to start in the (index + 1) of the previous loop (where the previous appointemnt was inserted)?

I have tried the whole day using @loop, @index, but with not success.

Any idea?

Please sign in or create an account to participate in this conversation.