Correctly outputting nested loops

I’m coding an event page and have a little issue with outputting data from my nested loops.

The agenda view is supposed to look like so:
Day 1:
Time - Title
- Meta
Time - Title
- Meta
Time - Title
- Meta

Day 2:
Time - Title
- Meta
Time - Title
- Meta
Time - Title
- Meta

My code is as below:

    $dates = CFS()->get( 'event_agenda' );
    if (!empty($dates)) {

    foreach ( $dates as $date ) {
        $date_block .= "<div class='date'>" . $date['event_day'] . "</div>";
        $sessions = $date['event_sessions'];
        
        foreach ( $sessions as $session ) {
            
            $session_time = $session['event_session_start_time'];
            
            foreach ( $session_time as $key => $label ) {
                $session_block .= "<div class='time'>" . $label . "</div><div class='meta'>" . $session['event_session_title'] . $session['event_session_description'] . "</div>";
            }
        }
        $agenda_block .= "<div class='agenda-block'>" . $date_block . $session_block . "</div>";
        highlight_string("<?php\n\$data =\n" . var_export($agenda_block, true) . ";\n?>");
        return $agenda_block;
    }	
}

The result of this is only a single day is shown:

I’m pretty sure that I’m generating the blocks in the correct place within the nested loops i.e. I need to get the dates first and then within each date, loop through each session and time and save them. Then output per date.

I’m confused why this is happening and any help would be much appreciated!

Hi @ibraheembcm,

First of all, you can probably get rid of the if here:

if (!empty($dates)) {

Note that, should $dates be empty the foreach wouldn’t be executed anyway.

Isn’t this showing you an error message? I don’t see where you are initializing the var $agenda_block.

You should probably re-initialize $session_block and $date_block in each iteration.

Also, for this kind of visualization you probably want to use a table instead of divs… that can be the problem you’re seeing.

Oh thanks, that’s a good point about the if statement!

I prefer to use CSS Flexbox for column/row layouts as I feel it’s just so much easier and responsive for web pages compared to tables.

I’ve tried initialising my variables but it never seems to make a difference (this is for a WordPress website). I believed that initialisation should be done from the start of the parent loop as that’s the convention I always use in my code:

    $dates = CFS()->get( 'event_agenda' );

    foreach ( $dates as $date ) {
        $agenda_block = "";
        $date_block = "";
        $session_block = "";
        $date_block .= "<div class='date'>" . $date['event_day'] . "</div>";
        $sessions = $date['event_sessions'];

        foreach ( $sessions as $session ) {
            $session_time = $session['event_session_start_time'];

            foreach ( $session_time as $key => $label ) {
                $session_block .= "<div class='session-block'><div class='time'>" . $label . "</div><div class='meta'><div class='title'>" . $session['event_session_title'] . "</div>" . $session['event_session_description'] . "</div></div>";
            }
        }
        $agenda_block .= "<div class='agenda-block'>" . $date_block . $session_block . "</div>";
        return $agenda_block;
    }	

Unfortunately, this makes no difference.

If you use bbcode [code][/code] tags or markdown (three back-ticks ```) around your code, it will all be displayed, as one piece, and color-highlighted. I have made this edit to your posts in this thread.

The reason for only one date worth of output is because you are returning from this code right before the closing } for the foreach ( $dates as $date ) { loop. So, only one $date value has been looped over.

There seems to be something odd… shouldn’t the return statement be outside of the main loop?

I had thought that to return the date for every iteration of the loop that the return statement should be at the end of the loop?
If I move it to outside of the parent loop…actually that worked! My fundamentals seem to still need work it seems!

Thank you, both of you, I’ve learnt something new today.

Sponsor our Newsletter | Privacy Policy | Terms of Service