Support

Account

Home Forums Add-ons Repeater Field Looking for a more neat solution to returning specific rows from a repeater

Solved

Looking for a more neat solution to returning specific rows from a repeater

  • Hi,

    I have a repeater field from which I need to return values from specific rows to different locations in my markup. I got my code to work, but I don’t believe it’s a neat solution, and was looking for something better.

    This is was I currently have:

    <?php
      $stripTagExcept = '<br> <strong> <em> <u> <del> <a> <img> <ul> <ol> <li> <span> <blockquote>';
      if( have_rows('home_steps_steps', 2) ) {
        while( have_rows('home_steps_steps', 2) ) { the_row();
          if( get_sub_field('home_steps_steps_id') == 1 ) {
            $stepIconOne = get_sub_field('home_steps_steps_icon');
            $stepTitleOne = get_sub_field('home_steps_steps_title');
            $stepParagraphOne = get_sub_field('home_steps_steps_paragraph');
            $stepPOneFormat = strip_tags($stepParagraphOne, $stripTagExcept);
          }
          if( get_sub_field('home_steps_steps_id') == 2 ) {
            $stepIconTwo = get_sub_field('home_steps_steps_icon');
            $stepTitleTwo = get_sub_field('home_steps_steps_title');
            $stepParagraphTwo = get_sub_field('home_steps_steps_paragraph');
            $stepPTwoFormat = strip_tags($stepParagraphTwo, $stripTagExcept);
          }
          if( get_sub_field('home_steps_steps_id') == 3 ) {
            $stepIconThree = get_sub_field('home_steps_steps_icon');
            $stepTitleThree = get_sub_field('home_steps_steps_title');
            $stepParagraphThree = get_sub_field('home_steps_steps_paragraph');
            $stepPThreeFormat = strip_tags($stepParagraphThree, $stripTagExcept);
          }
          if( get_sub_field('home_steps_steps_id') == 4 ) {
            $stepIconFour = get_sub_field('home_steps_steps_icon');
            $stepTitleFour = get_sub_field('home_steps_steps_title');
            $stepParagraphFour = get_sub_field('home_steps_steps_paragraph');
            $stepPFourFormat = strip_tags($stepParagraphFour, $stripTagExcept);
          }
          if( get_sub_field('home_steps_steps_id') == 5 ) {
            $stepIconFive = get_sub_field('home_steps_steps_icon');
            $stepTitleFive = get_sub_field('home_steps_steps_title');
            $stepParagraphFive = get_sub_field('home_steps_steps_paragraph');
            $stepPFiveFormat = strip_tags($stepParagraphFive, $stripTagExcept);
          }
        }
      }
    ?>

    I basically added an addition field in which I added a unique ID (1-5 for now) and use this field to return the values in a variable.

    Is there a neater way to do this? I have only been working with PHP for the last 2 months, so I wouldn’t know of anything better at the moment. I also looked up the ACF documentation, but it didn’t get me really far for once. Thanks in advance!

  • I would use a switch statement, they are cleaner when you have many possiblities

    
    $steps = get_sub_field('home_steps_steps_id');
    switch ($steps) {
      case 1:
        // do something
        break;
      case 2:
        // do something
        break;
      case 3:
        // do something
        break;
      case 4:
        // do something
        break;
      case 5:
        // do something
        break;
    }
    

    https://www.php.net/manual/en/control-structures.switch.php

  • Thanks John! I am going to try this.

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

The topic ‘Looking for a more neat solution to returning specific rows from a repeater’ is closed to new replies.