Support

Account

Home Forums ACF PRO Echo closing tag after the last row, if rows are not empty Reply To: Echo closing tag after the last row, if rows are not empty

  • Hey again,
    apparently my function still missed something: it couldn’t handle posts where the first or the last field in the repeater (a step) had an empty sub-field (step title). I figured it out somehow (please see below) but now I have a different problem which I cannot find a solution for – I don’t want the whole loop to happen if all sub-fields are empty. the thing is, it’s impossible to get all sub-fields outside the loop in order to count the empty ones before the loop has started.
    This is what I have now:

    //table of content for instructuble posts
    function itamar_index_for_instructubles() {
    		// check if the instructuble has steps
    		if( have_rows('שלבים') ):
    			$i=0; //counts all iterations, inside the loop
    			$x=0; //counts only iterations with a step title, inside the loop
    			$rows = get_field('שלבים');
    			$total=count($rows); //counts the total number of iterations outside the loop to determine the last one
    			// loop through the steps
    			while ( have_rows('שלבים') ) : the_row();
    				$step_title = get_sub_field('שם_השלב');
    					$i++;
    					// first iteration
    					if($i == 1) :
    						//open tags for the table of content
    						$indexed = '<div class="info-table" id="index"><div class="title">תוכן עניינים<span id="oc-index">-</span></div><div class="body"><ul class="indexed-content">';
    					endif;
    					if($step_title!='' && $i >= 1) :
    					$x++;
    					$indexed .= '<li>'.$x.'. <a href="#anc'.$x.'">'.$step_title.'</a></li>' ;
    					endif;
    					// last iteration 
    					if($i == $total) :
    						//closing tags for the table of content
    						$indexed .= '</ul></div><div class="bottom"></div></div>';
    					endif;			
    			endwhile;
    			echo $indexed;
    		endif;
    }

    And what I would like to do is something like:

    
    /**** before the loop has started ****/
    $total=0;
    $sub_fields = //a function to get all subfields under a given repeater
    foreach($sub_fields as $sub_field){
        if ($sub_field->content != ""){
            $total++;
        }
    }
    /*****/
    /**** inside the loop ****/
    // first iteration
    if($total != 0 && $i == 1) :
    	//open tags for the table of content
    	$indexed = '<div class="info-table" id="index"><div class="title">תוכן עניינים<span id="oc-index">-</span></div><div class="body"><ul class="indexed-content">';
    endif;
    /*****/
    

    Is there a way to get that done?

    Thanks a lot,
    Itamar

    EDIT:
    I found the way:

    //table of content for instructuble posts
    function itamar_index_for_instructubles() {
    		// check if the instructuble has steps
    		if( have_rows('שלבים') ):
    			$rows = get_field('שלבים');
    			$total_steps = count($rows); //counts the total number of steps outside the loop to determine the last one, outside the loop
    			$total_titles = 0; //counts the total number of non-empty step titels, outside the loop
    			if($rows){
    				foreach($rows as $row){
    					if($row['שם_השלב'] != ""){$total_titles++;}
    				}
    			}
    			// loop through the steps
    			if($total_titles != 0) :
    				$i=0; //counts all iterations, inside the loop
    				$x=0; //counts only iterations with a step title, inside the loop
    				while ( have_rows('שלבים') ) : the_row();
    					$step_title = get_sub_field('שם_השלב');
    						$i++;
    						// first iteration
    						if($i == 1) :
    							//open tags for the table of content
    							$indexed .= '<div class="info-table" id="index"><div class="title">תוכן עניינים<span id="oc-index">-</span></div><div class="body"><ul class="indexed-content">';
    						endif;
    						if($step_title!='' && $i >= 1) :
    						$x++;
    						$indexed .= '<li>'.$x.'. <a href="#anc'.$x.'">'.$step_title.'</a></li>' ;
    						endif;
    						// last iteration 
    						if($i == $total_steps) :
    							//closing tags for the table of content
    							$indexed .= '</ul></div><div class="bottom"></div></div>';
    						endif;			
    				endwhile;
    			endif;
    			echo $indexed;
    		endif;
    }