Support

Account

Home Forums Add-ons Repeater Field List all repeater subfield values for a post (preferably comma separated)

Solved

List all repeater subfield values for a post (preferably comma separated)

  • This seems like such an easy thing to accomplish, so I’m sure I’m just missing it somehow.

    What I’m looking to do is list all repeater field (line_chart_all_months) subfield values (line_chart_month) as a comma separated array (each value in single quotes).

    For example, each subfield is a text input with a value such as Aug-22 (a total of 12, ending in Jul-23). The desired return needs to look like this:

    ‘Aug-22′,’Sep-22′,’Oct-22′,’Nov-22′,’Dec-22′,’Jan-23′,’Feb-23′,’Mar-23′,’Apr-23′,’May-23′,’Jun-23′,’Jul-23’

    How is this accomplished?

    I’ve tried:

    <?php if(have_rows('line_chart_all_months') ): while(have_rows('line_chart_all_months') ) : the_row(); the_sub_field('line_chart_month');
    endwhile; else : endif; ?>

    But it returns this:
    Aug-22Sep-22Oct-22Nov-22Dec-22Jan-23Feb-23Mar-23Apr-23May-23Jun-23Jul-23

    How do I get those separated with commas and single quotes for each value like above?

    Thanks kindly in advance.

  • The very last value should have no comma.

  • 
    <?php 
      $dates = array();
      if (have_rows('line_chart_all_months')) {
        while (have_rows('line_chart_all_months')) {
          the_row();
          $dates[] = get_sub_field('line_chart_month');
        }
      }
      echo implode(', ', $dates);
    ?>
    
  • Thanks very much for the help John!

    This almost gets me there, however, I’m missing the quotes around each month. Currently what gets returned is this:

    Aug-22, Sep-22, Oct-22, Nov-22, Dec-22, Jan-23, Feb-23, Mar-23, Apr-23, May-23, Jun-23, Jul-23

    However, I need it to have quotes around each date as well (with the comma outside) like this:

    ‘Aug-22′,’Sep-22′,’Oct-22′,’Nov-22′,’Dec-22′,’Jan-23′,’Feb-23′,’Mar-23′,’Apr-23′,’May-23′,’Jun-23′,’Jul-23’

  • 
    echo "'".implode("', '", $dates)."'";
    
  • Perfection! Thank You John.

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

You must be logged in to reply to this topic.