Skip Navigation

[Resolved] Need to output some text depending on the output of a calculation

This support ticket is created 9 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by dbarber 9 years, 9 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#173865

Hi there

I need to be able to take the output of a calculation and then use it to output a couple of words, depending on what the calculated total is - e.g., if calculated total is:

- Less than 18% then I need the output to show as A
- Between 18% and 22% then I need the output to show as B
- Over 22% then I need the output to show as C

This is the calculation that I need to use to to compare against the above figures.

[wpv-calculate evaluate="[types field="detached-sales-current" id="$market-updates"][/types] / [types field="detached-active-listings-current" id="$market-updates"][/types] * 100 "]%

I'm guessing that this can be done using wpv-if but it's doing my head in trying to figure out how.

Any guidance would be greatly appreciated.

Many thanks

#174033

Hi dbarber,

I assume you are using such a custom shortcode in your theme/functions.php:
https://toolset.com/forums/topic/need-to-limit-the-number-of-digits-output-from-a-calculation/#post-173568

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
return number_format(wpv_condition($atts), 2);
}

Please try modify it like this:

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
	extract( shortcode_atts( array(
	      'evaluate' => '',
	      'format_filter' => 'number_format_filter'
     ), $atts ) );
	$res = wpv_condition($atts);
	$res = apply_filters($format_filter, $res);
	return $res;
}

add_filter('number_format_filter', 'number_format_filter_func');
function number_format_filter_func($v)
{
	return $res = number_format($v, 2);
}

add_filter('compare_format_filter', 'compare_format_filter_func');
function compare_format_filter_func($v)
{
	$res = 'C';
	if($v<18)$res= 'A';
	if($v>18 && $v<22)$res= 'B';
	//here you can add more conditions
	return $res;
}

Modify the shortcode in your content like this:

[wpv-calculate evaluate="[types field="detached-sales-current" id="$market-updates"][/types] / [types field="detached-active-listings-current" id="$market-updates"][/types] * 100 " format_filter="compare_format_filter"]

Add attribute format_filter="compare_format_filter" in the shortcode

#174318

Hi Luoy

Thank you very much for this!

Tested and working perfectly.

Cheers

Dawson