Support

Account

Home Forums General Issues Testing array values against Repeater fields

Solved

Testing array values against Repeater fields

  • I have an array of domains, like so:

    $doms = array("gmail", "yahoo", "hotmail" ...etc);

    I also have a Repeater field that contains some sub fields, one of which is called ’email_domain_name’.

    What I am trying to do is foreach loop through the array, and then test if each array value is in one of the repeatable fields. I’m unable to get this to work. So far I have this (‘user_1’ is the id of the user whos profile contains the Repeater fields, ‘bad_email_identifier’ is the repeater field name):

    foreach($doms as $dom){
        if( have_rows('bad_email_identifier', 'user_1') ):
            while( have_rows('bad_email_identifier','user_1') ): the_row();
                if (the_sub_field('email_domain_name' == $dom)) {
                    echo the_sub_field('email_domain_name');
                }
            endwhile;
        endif;
    }

    There’s something obvious I’m missing but I just can’t spot it.

  • dont use the_sub_field inside a “if-check” use get_sub_field instead.
    also dont use echo the_sub_field echo is not necessary there.
    because the_sub_field/the_field are functions that echo per se.
    and get_sub_field/get_field only get/check a value that need to be saved inside a variable for later using.

    try to use one of this codeparts instead of yours:

    if (get_sub_field('email_domain_name') == $dom) {
                    the_sub_field('email_domain_name');
                }
    $emaildom = get_sub_field('email_domain_name');
    if ($emaildom == $dom) {
                    echo $emaildom;
                }
  • Second one worked perfectly, thanks 🙂

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

The topic ‘Testing array values against Repeater fields’ is closed to new replies.