Support

Account

Home Forums Add-ons Repeater Field Splitting fields into table rows

Solved

Splitting fields into table rows

  • Hi, I’d like to split my repeater fields into two rows. Here’s what I have so far:

    <table>
    
    <?php while( have_rows('my_repeater') ): the_row(); 
    
    // vars
    $field1 = get_sub_field('field1');
    $field2 = get_sub_field('field2');
    $field3 = get_sub_field('field3');
    ?>
    
     <tr>
     // All $field1 and $field2 fields in this row each wrapped in <td> tags
      <td><?php echo $field1; ?></td>
      <td rowspan="2"><?php echo $field2; ?></td>
     </tr>
     
    <tr>
     // All $field 3 in this row wrapped in <td> tags
     <td><?php echo $field3; ?></td>
    </tr>
    
    <?php endwhile; ?>
    
    </table> 

    Is this possible and would anyone be able to help point me in the right direction of achieving this please?

    Many thanks

  • Hi @ordog ,

    I beleive your code should work just fine to create two rows out of every repeater row.

    However, you will need to take notice the table markup so that each row will have an equal number of rows or combine the cell.

    Hopefully this helps 🙂

  • Hi James

    The code above does work but it doesn’t achieve what I need. I’ll try and explain again.

    Based on the code above I need the output to be:

    <table>
    <tr>
     // All $field1 and $field2 fields in this row each wrapped in <td> tags
      <td>This is field 1 text</td>
      <td rowspan="2">This is field 2 text</td>
      <td>This is another loop of field 1 text</td>
      <td rowspan="2">This is another loop of field 2 text</td>
     </tr>
     
    <tr>
     // All $field 3 in this row wrapped in <td> tags
     <td>This is field 3 text</td>
     <td>This is another loop of field 3 text</td>
    </tr>
    </table>

    I basically need it to loop through the all $field1 and $field2 fields and then start looping through all the $field3 ones. Hopefully that helps explain things a little better?

    Many thanks

  • Hu @ordog ,

    Thanks a bundle for getting back to us.

    In that case, you could create two while loops. The first will access all the field1 & field2 values.

    Then you will access the field3 from the second loop.

    Let me know if this is what you are looking for.

  • Hi @acf-support

    That sounds like what I am looking for. How would I go about implementing this please?

  • Hi @ordog ,

    Here is a sample using two while loops to show the data. Let me know if it helps.

    <table>
    
    <?php while( have_rows('my_repeater') ): the_row(); 
    
    // vars
    $field1 = get_sub_field('field1');
    $field2 = get_sub_field('field2');
    ?>
    
     <tr>
     // All $field1 and $field2 fields in this row each wrapped in <td> tags
      <td><?php echo $field1; ?></td>
      <td rowspan="2"><?php echo $field2; ?></td>
     </tr>
     
    <tr>
    
    <?php endwhile; ?>
    
    <?php while( have_rows('my_repeater') ): the_row(); 
    $field3 = get_sub_field('field3');
    ?>
    
     
    <tr>
     // All $field 3 in this row wrapped in <td> tags
     <td><?php echo $field3; ?></td>
    </tr>
    
    <?php endwhile; ?>
    
    </table>

    Hopefully this helps. 🙂

  • Of course! It’s so simple and obvious now I look at it, I don’t know why I couldn’t get my head around it before.

    Thank you!

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

The topic ‘Splitting fields into table rows’ is closed to new replies.