Support

Account

Home Forums Add-ons Repeater Field Repeater + Show lowest rate by term

Solving

Repeater + Show lowest rate by term

  • Hello,

    Using the repeater to store dozens of mortgage rates, some with the same term.

    I then use PHP to query/filter/sort the lowest rate for each term:

    <?php
    		
    	$array2 = get_field('acf_rates_fixed');
    	$arr = array_merge($array2);
    
    	$new_arr = array();
    
    	foreach($arr as $val){
    				
    		if(isset($new_arr[$val['acf_rates_term']])){
    			$cur_rate = $new_arr[$val['acf_rates_term']]['acf_rates_rate'];
    			$new_arr[$val['acf_rates_term']]['acf_rates_rate'] = ($cur_rate > /* > is low and < is high */ $val['acf_rates_rate']) ? $val['acf_rates_rate'] : $cur_rate;
    		} else {
    			$new_arr[$val['acf_rates_term']] =  $val;
    		}
    		
    	}
    
    	ksort($new_arr);
    	$result = array_values($new_arr);
    	
    		foreach($new_arr as $result) { ?>
    		
    		<!-- these are from the correct row -->
    		
        <?php echo $result['acf_rates_rate']?> <br />
        <?php echo $result['acf_rates_type']?> <br />
        <?php echo $result['acf_rates_term']?> <br />
        
        <!-- these are not from the correct row -->
        
        <?php echo $result['acf_rates_comments']?> <br />
        <?php echo $result['acf_rates_amortization']?>
    
    <?php	} ?>

    My code shows the lowest rate for each term, but any other fields in that row are from an entirely different row.

    The code fails if I have 3+ x 5 year fixed rates, any field other then “rate, type, term” are correct, but the rest of the fields from that repeater row are incorrect.

    I assume I have to add them back into the new array, but can’t wrap my head around this one.

    Any suggestions?

  • Job posted on codeable under “WP ACF + Repeater + Show lowest rate by term (semi works)”

  • UPDATED QUESTION

    Using the repeater to store mortgage rates, some with the same term.

    What the PHP dose:

    At some point, the code sorts ALL rows/array by mortgage term (1,2,3,4,5,10 years).

    Then if the arrays have more then 1 x term (example 10 x 5-year), it finds the term with the lowest rate and only displays that rate/term/type.

    This isn’t just an issue with 5-year rates, we could have 5000 x 1-year rates and 40 x 3-year rates and the sort/filter will always show the lowest rate for each term.

    This part of the code has always worked fine, that’s why it has been so hard to troubleshoot.

    <?php
    		
    	$arr = get_field('acf_rates_fixed');
    
    	$new_arr = array(); // not sure what this dose
    
    	foreach($arr as $val){
    		
    		$val['acf_rates_rate'];// not sure what this dose
    	
    		if(isset($new_arr[$val['acf_rates_term']])){
    			$cur_rate = $new_arr[$val['acf_rates_term']]['acf_rates_rate'];
    			$new_arr[$val['acf_rates_term']]['acf_rates_rate'] = ($cur_rate > /* > is low and < is high */ $val['acf_rates_rate']) ? $val['acf_rates_rate'] : $cur_rate;
    		} else {
    			$new_arr[$val['acf_rates_term']] =  $val;
    		}
    		
    	}
    
    	ksort($new_arr); // not sure what this dose
    	$result = array_values($new_arr); // not sure what this dose
    	
    		foreach($new_arr as $result) { ?>
    		
        <!-- these 3 values show perfectly -->
        
        acf_rates_rate = <?php echo $result['acf_rates_rate']?> <br />
        acf_rates_type = <?php echo $result['acf_rates_type']?> <br />
        acf_rates_term = <?php echo $result['acf_rates_term']?> <br />
        
        <!-- these show from another row --->
        
        acf_rates_comments = <?php echo $result['acf_rates_comments']?> <br />
        acf_rates_restrictions = <?php echo $result['acf_rates_restrictions']?> <br />
        acf_rates_occupancy = <?php echo $result['acf_rates_occupancy']?> <br />
        acf_rates_amortization = <?php echo $result['acf_rates_amortization']?> <br />
    
    <?php	} ?>

    What the issue is:

    Anytime it detects a duplicate term and finds the lowest rate, any keys after “rate/term/type” are from an entirely different rate, but keys from a similar term.

    Any suggestions?

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

The topic ‘Repeater + Show lowest rate by term’ is closed to new replies.