Support

Account

Home Forums Front-end Issues if field has value within Field Group conditional statement Reply To: if field has value within Field Group conditional statement

  • It looks like you have several repeaters rather than a single repeater. You will need to check each repeater to see if it has anything before creating the <table></table>

    There are a couple ways that you can do this

    1

    
    <?php
    
    if( get_field('product_watts') && get_field('product_watt_replacement')) {
    echo '<table>';
    
    if( get_field('product_watts') ) {
    
        echo '<tr><td>' . '<strong>Wattage: </strong></td><td>'; 
        while ( have_rows('product_watts') ) : the_row();
        	 $array1[] = get_sub_field('watts')  .'w'; 
        endwhile;
        	$watts = implode(', ', $array1);
    
        echo $watts .'</td></tr>'; 
    }
    
    if( get_field('product_watt_replacement') ) {
    
        echo '<tr><td>' . '<strong>Wattage Replacement: </strong></td><td>'; 
        while ( have_rows('product_watt_replacement') ) : the_row();
        	 $array2[] = get_sub_field('watt_replacement')  .'w'; 
        endwhile;
        	$watt = implode(', ', $array2);
    
        echo $watt .'</td></tr>'; 
    }
    
    echo '</table>';
    } // end if all fields have values
    
    ?>
    

    The other way would be to loop though each field and build the html and then test to see if there is any content, something like this

    
    <?php
    
    $content = '';
    if( get_field('product_watts') ) {
    
        $content .= '<tr><td>' . '<strong>Wattage: </strong></td><td>'; 
        while ( have_rows('product_watts') ) : the_row();
        	 $array1[] = get_sub_field('watts')  .'w'; 
        endwhile;
        	$watts = implode(', ', $array1);
    
        $content .=  $watts .'</td></tr>'; 
    }
    
    if( get_field('product_watt_replacement') ) {
    
        $content .=  '<tr><td>' . '<strong>Wattage Replacement: </strong></td><td>'; 
        while ( have_rows('product_watt_replacement') ) : the_row();
        	 $array2[] = get_sub_field('watt_replacement')  .'w'; 
        endwhile;
        	$watt = implode(', ', $array2);
    
        $content .=  $watt .'</td></tr>'; 
    }
    
    if ($contend) {
     echo '<table'>',$content,'</table>';
    }
    ?>