Skip Navigation

[Resolved] Need to perform 4 different calculations on the same website

This support ticket is created 9 years, 12 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 16 replies, has 2 voices.

Last updated by dbarber 9 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#176738

I've started a few different threads that, at the time, I did not realize overlapped one another.

After reading the (excellent) responses, figured that the best way to get the help I need would be to start a fresh thread, outlining everything I need to do and referencing the other related threads I started (in case it helps).

I guess that I just didn't really know the right question to ask until I got the responses that led me to realize that my "separate" questions were actually all closely related.

Anyway, here goes...

Need #1
I need to perform a simple calculation (A / B) that outputs as a percentage, limited to 2 decimal points, and colors any result that's less than 0.00 in red. For reference, here's the thread I started on this topic: https://toolset.com/forums/topic/need-to-limit-the-number-of-digits-output-from-a-calculation/

Need #2
I need to perform a more complex calculation ((A - B) / B) that outputs as a percentage, limited to 2 decimal points, and colors any result that's less than 0.00 in red. For reference, here's the thread I started on this topic: https://toolset.com/forums/topic/another-question-about-calculations/#post-176454

Need #3
I need to be able to apply 2 different calculations on 2 completely different sets of data that outputs a line of text based on a condition. For reference, here's the thread I started on this topic: https://toolset.com/forums/topic/need-to-output-some-text-depending-on-the-output-of-a-calculation/#post-174033

Data set 1:

If X is less than 18 then A
If X is between 18 and 22 then B
If X is greater than 22 then C

Data set 2:

If X is less than 5 then A
If X is between 5 and 10 then B
If X is greater than 10 then C

So, just to be clear on Need #3, I'd need a shortcode to apply the first formula to one set of data and a different shortcode to apply to a completely different set of data.

I know that the answers to this exist in the responses I have received. I just don't know how to put them together. I've tried and have not had any success.

Please advise.

Many thanks

Dawson

#177061

Hi Dawson,

I think all your needs can be put into one shortcode,
Please try like this:
1) modify the codes in your theme/functions.php, 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)
{
	$class = "";
	if($v<0)$class = "my-red";
	return $res = '<div class="' . $class . '">' . number_format($v, 2) . '</div>';
}

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

add_filter('compare_format_filter_2', 'compare_format_filter_func_2');
function compare_format_filter_func_2($v)
{
	$res = '';
	if($v<5)$res= 'A';
	if($v>5 && $v<10)$res= 'B';
	if($v>10)$res= 'C';
	//here you can add more conditions
	return $res;
}

Add CSS code in your theme/style.css:

div.my-red{
color: red;
}

2) in different case, you use the shortcode with different attribute:
a) for Need #1:
put the shortcode into your content like this:

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

The structure is:
[wpv-calculate evaluate='A / B']

b) for Need #2
put the shortcode into your content like this:

[wpv-calculate evaluate='([types field="detached-sales-current"][/types] – [types field="detached-active-listings-current"][/types])/ [types field="detached-active-listings-current"][/types]']

The structure is:
[wpv-calculate evaluate='(A – B) / B']

c) use different attribute "format_filter" for different case
Data set 1:
put the shortcode into your content like this structure:
[wpv-calculate evaluate='X' format_filter="compare_format_filter]
Data set 2:
[wpv-calculate evaluate='X' format_filter="compare_format_filter_2]

You can try create a post with codes in it and test:

[wpv-calculate evaluate='6/101*100']

--------------------------------------

[wpv-calculate evaluate='(1-14) / 102*100']

--------------------------------------

[wpv-calculate evaluate='15' format_filter="compare_format_filter]

--------------------------------------

[wpv-calculate evaluate='23' format_filter="compare_format_filter_2]
#177443

Hi Luoy

Thank you very much for this. We've almost got this one solved.

I modified functions.php as you advised above and all of the following examples/tests work:

[wpv-calculate evaluate='6/101*100']
 
--------------------------------------
 
[wpv-calculate evaluate='(1-14) / 102*100']
 
--------------------------------------
 
[wpv-calculate evaluate='15' format_filter="compare_format_filter"]
 
--------------------------------------
 
[wpv-calculate evaluate='23' format_filter="compare_format_filter_2"]

However when I try to use the shortcodes, the output does not change, regardless of the input.

I've tried the following (and various different variations of it) without success:

[wpv-calculate evaluate="([types field="c-sales-current" id=""][/types] - [types field="c-active-listings-current" id=""][/types]) / [types field="c-active-listings-current" id=""][/types]" format_filter="compare_format_filter"]

I've been staring at the code for a while now and can't figure this one out.

#177489

Hi Luoy

I forgot to ask if there is a way for the % symbol to show up automatically as opposed to being added outside of the shortcode?

The reason I ask is that I swapped the div for a span so that red-colored text appears inline, but, of course, the % symbol gets added outside of the shortcode and so the class does not affect it.

Perhaps there's something that can be added to the functions.php to make this happen?

Cheers

Dawson

#177528

Please modify the codes in your theme functions.php, as 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)
{
	if(!is_numeric($v)) return $v;
	$class = "";
	if($v<0)$class = "my-red";
	return $res = '<div class="' . $class . '">' . number_format($v, 2) . ' %</div>';
}

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

add_filter('compare_format_filter_2', 'compare_format_filter_func_2');
function compare_format_filter_func_2($v)
{
	$res = '';
	if($v<5)$res= 'A';
	if($v>5 && $v<10)$res= 'B';
	if($v>10)$res= 'C';
	//here you can add more conditions
	return $res;
}

And test again, if problem still exists, please post the shortcode you put in content, and the custom field value you are using, I need debug it in my localhost

#177957

Hi Luoy

Still having a problem.

Here's the shortcode I'm using:

[wpv-calculate evaluate="[types field="t-current" id=""][/types] / [types field="t-active" id=""][/types]" format_filter="compare_format_filter"]

Value of t-current is 36

Value of t-active is 215

The basic calculation and the more complex formula both work perfectly (as does the red text and %)..

It's just the format_filter that's not outputting correctly.

Thanks

Dawson

#178073

I test the values you mentioned above, it output result "A"
36/215 = 0.16744186046512
According the "Data set 1:" you mentioned above, I think it works fine.

Could you describe more detail about the question? thanks

#178730

Hi Luoy

Let me try this on a fresh installation and get back to your with my findings.

#179059

Is this problem resolved? if you still need assistance, please send the login details to my email: luo.y@onthegosystems.com
Also point out the problem page URL, and where can I edit your php codes. and include this thread URL for reference
Thanks

#179276

Hi Luoy

Email with all details sent.

Cheers

Dawson

#179493

Modified your codes as this:

<p>[wpv-calculate evaluate="([types field="fvreb-sales-balanced-market" output="raw"][/types] / [types field="fvreb-listings-balanced-market" output="raw"][/types])*100" format_filter="compare_format_filter"]</p>

Please check if it is what you want

#179559

I tried it but it still wasn't working properly, so I took a look a the code in the functions.php and it occurred to me that maybe I was asking the impossible:

If under 18 then X
If between 18 and 22 then Y
If over 22 then Z

So I modified the code in the functions.php thusly:

add_filter('compare_format_filter', 'compare_format_filter_func');
function compare_format_filter_func($v)
{
    $res = '';
    if($v<18)$res= 'FVREB Buyer\'s Market';
    if($v>17.99 && $v<22)$res= 'FVREB Balanced Market';
    if($v>21.99)$res= 'FVREB Seller\'s Market';
    //here you can add more conditions
    return $res;
}

I've tested with 17.99, 18, 21.99, 22 - and number in between - and it's all working.

Do you foresee the possibility of any issues with the above?

And thank you very much for your help and your patience!

#179712

I have modified your codes as this:

...
function compare_format_filter_func($v)
{
    $res = '';
    if($v<18)$res= 'FVREB Buyer\'s Market';
    if($v>=18 && $v<=22)$res= 'FVREB Balanced Market';
    if($v>22)$res= 'FVREB Seller\'s Market';
    //here you can add more conditions
    return $res;
}
 
add_filter('compare_format_filter_2', 'compare_format_filter_func_2');
function compare_format_filter_func_2($v)
{
    $res = '';
    if($v<14)$res= 'CADREB Buyer\'s Market';
    if($v>=14 && $v<=21)$res= 'CADREB Balanced Market';
    if($v>21)$res= 'CADREB Seller\'s Market';
    //here you can add more conditions
    return $res;
}

Please check if it is what you want

#180190

Hi Luoy

I'm now getting the same output for each condition, regardless of the inputs.

Dawson

#181325

Sorry for the delay answer, I am check it in your website, will feedback if there is any found

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.