Support

Account

Home Forums Add-ons Flexible Content Field while ( have_rows() ) within while ( have_rows() ) …possible?

Solving

while ( have_rows() ) within while ( have_rows() ) …possible?

  • Hi

    I have a bit of an odd situation, but I really need to get this working with my flexible content fields.

    I want to loop have_rows() and within each of them, list out the have_rows() again.

    Like so:

    <?php
    if (have_rows('blocks')): 
    
      while (have_rows('blocks')): the_row();
        echo get_sub_field('title');
        echo 'All rows...';
    
        // Creates an endless loop.
        while (have_rows('blocks')): the_row();
          echo get_row_layout();
        endwhile;
    
      endwhile;
    
    endif;
    ?>
    

    However, the above appears to create an endless loop.

    Any help in achieving a listing of all the rows on the page within each of the rows?

    Help much appreciated!

    Thanks
    Michael

  • Do the first and second loops refer to the same field or do you have a flexible content field inside of a repeater?

  • Hi John

    There are no repeaters in this scenario, just one flexible content field called ‘blocks’.

    I need to loop ‘blocks’ in each iteration of ‘blocks’ itself.

    However, trying the above just endlessly loops. Not sure how I can get around it.

    Thanks for looking at this!

  • You can’t create a nested loop on the same field. Honestly, I don’t know why it’s creating an endless loop, it should not be. What your doing would be similar to doing something like this

    
    <?php 
      
      $x = 0;
      while ($x<10) {
        $x++;
        // code here only happens once
        echo 'outer loop ',$x,'<br />';
        while ($x<10) {
          $x++;
          // code here happens 9 times
          echo 'inner loop ',$x,'<br />';
        }
      }
      
    ?>
    

    I can’t really tell you how to correct what you’re doing, I’d need to know more about why you are trying to nest loops on the same repeater. More than likely there is some other way to get the results you’re looking for.

  • Hi John

    Yes, it’s an unconventional situation but let me try to explain:

    I am trying to build a ‘jump navigation’ to each of the rows on my page dynamically. This has to sit within one of the ‘blocks’ created.

    So, essentially, one of the blocks in this flexible content field, needs to list each of the blocks so that it can navigate between them.

    Hope that makes sense? Really hope there’s a solution to this!

    Thanks

  • Can you give me an example of what the jump nav will look like?

  • If your jump menu is number based and does not show any of the content from each block then you can simply get the number of rows in the field.

    
    $block_count = count(get_field('blocks'));
    

    If you’ll be showing content in the jump menu from each block, for example, if each block has a title and you need that title to be part of the menu, then you will need to loop through the rows twice. The first time gathering information you need for the menu and the second time to display each block.

    
    $blocks = array();
    if (have_rows('blocks')) {
      // first loop
      while (have_rows('blocks')) {
        the_row();
        $blocks[] = get_sub_field('title');
      }
      // second loop
      while (have_rows('blocks')) {
        the_row();
        // output the row
      }
    }
    

    There are also other ways you can do this, for example, if you use

    
    $blocks = get_field('blocks');
    // blocks holds nested array of all fields and subfields
    echo '<pre>'; print_r($blocks); echo '</pre>';
    

    Using this array you could build a nested loop the way you want to build it because nested foreach loops can reference the same array parameter and they will loop separately.

    
    foreach ($array as $index => $value) {
      // this will happen as many times as there are array elements
      foreach ($array as $nested_index => $nested_value) {
        // this will happen {number of array elements} squared
      }
    }
    

    Hope some of this information helps.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘while ( have_rows() ) within while ( have_rows() ) …possible?’ is closed to new replies.