Support

Account

Home Forums Front-end Issues if statement issue subfields in group^field

Solved

if statement issue subfields in group^field

  • Hi everybody!
    It’s the first time I write on this forum and I’m looking for some help with displaying ACF fields in frontend.

    I have a group field called “Total” with some number subfields “2010,2011,…”
    The idea is to display a table with the subfields values when at least one of these subfields has a value and when there are no values, no table.

    The problem with the code I have for the moment is when there are no values, I have an empty table.

    Here is the code:

    $field = get_field_object('total', $curauth);
    		$values = get_field('total', $curauth);
    		if(!empty($values)) {
    			echo '<div class="column one"><h4>Operating budget in euros</h4>
    	<table>
    	<thead>
    		<tr>
    			<th>2010</th>
    			<th>2011</th>
    			<th>2012</th>
    			<th>2013</th>
    			<th>2014</th>
    			<th>2015</th>
    			<th>2016</th>
    			<th>2017</th>
    			<th>2018</th>
    			<th>2019</th>
    		</tr>
    	</thead>
    	<tbody>';
    			echo '<tr>';					
    			foreach ($values as $value) {
    				echo '<td>';
    				echo $value;
    				echo '</td>';}
    			echo '</tr>';
    			echo '</tbody>
    </table></div>';
    	
    }

    Any help is welcome! Thanks

  • The ACF group field is a repeater field that always has one row. The value of the field will never be empty. There are several ways you can check this.

    
    // check each value in a loop;
    $values = get_field('total', $curauth);
    $empty = true;
    if (is_array($values)) {
      foreach ($values as $field_name => $value) {
        if ($value) {
          $empty = false;
          break;
        }
      }
    }
    if (!$empty) {
      //.....
    }
    
    
    // check using array_filter()
    $values = get_field('total', $curauth);
    $empty = true;
    if (is_array($values) && count(array_filter($values)) != 0) {
      $empty = false;
    }
    if (!$empty) {
     //...
    )
    
  • Hi John,
    I just tried the first option and it’s working perfectly! I’m a beginnner and this kind of things takes me a lot of time to go through, thanks a lot for your help!

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

The topic ‘if statement issue subfields in group^field’ is closed to new replies.