Support

Account

Home Forums ACF PRO Show lowest value

Solved

Show lowest value

  • Hello

    I use ACF for differents formulas in my post.
    capture here

    I wish in my category page, display the services: if I have multiple formulas show the lawest price and hightest price for each of them and if I have one formula juste display the price

    something like this:

    Capture here

    this is my actual code:

    <div class="tarif">
    <?php				
    if( have_rows('formule') ):
    	while ( have_rows('formule') ) : the_row(); ?>
    		<p><?php  $tarif_formule= the_sub_field('prix_de_la_formule'); ?>
    			<br />
    			<?php echo the_sub_field('duree_prestation'); ?></p>
    
           <?php endwhile;
    endif;?>
    </div>

    there is a way to do it ?

    thank you 🙂

  • I would probably do something like this.

    
    if (have_rows('formule')) {
      $high = 0;
      $low = 999999; // set to something higher than the highest can possibly be
      echo '<p>';
      while (have_rows('formule')) {
        the_row();
        the_sub_field('prix_de_la_formule');
        echo '<br />';
        $value = get_sub_field('duree_prestation');
        if ($value > $high) {
          $high = $value;
        }
        if ($value < $low) {
          $low = $value;
        }
      }  // end while have rows
      echo $low;
      if ($low != $high) {
        echo ' to ',$high;
      }
      echo '</p>';
    } // end if have_rows
    
  • thank you for your answer :),

    But it doesn’t return what I wish. I just edit my code to explain precisly; may be it will be cleaner:

    <?php if (have_rows('formule')) {
        
    					  while (have_rows('formule')) {
    						the_row();
    						the_sub_field('prix_de_la_formule');
    						echo '<br />';
    						$value = get_sub_field('prix_de_la_formule');
    						$count = count($value); // count number of values in the page
    						
    						if ($count = 1) {
    						  echo $value; // get the value
    						}
    						elseif  ($count > 1) {
    						  echo 'from .$lowestValue. to .$hightestValue' ;  // get "the lowest value"
    						}
    					  }  // end while have rows
    					 
    					} // end if have_rows ?>
    					

    It’s not working of course.
    With this one “$value” return a loop repeating the only one value, it’s not what I want.

    For lowest and hightest value, it’s not the same prices all the time (depends formula), so I wish make something picking up the values (in table mabe be ?) and compare all of them to display lowest and hightest from the results

  • That is why my code is the way it is. You need to calculate the high and low in the loop. You will not know what the high and low is until the loop is completed and you’ve looked at every row.

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

You must be logged in to reply to this topic.