Skip Navigation

[Resolved] Calculate a running total on a view

This support ticket is created 6 years, 8 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 7 replies, has 2 voices.

Last updated by Nigel 6 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#622923
Capture.JPG

I have built a rudimentary budget system in toolset which allows users to create budget items and allocate payments to each item. I run some simple calculations to show remaining balance on a budget item using this shortcode:

function calculate_shortcode($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval('return ' . $content . ';');
return round($content, 2);
}

What I would like to do next is to calculate the total of all budget items and present that to the user at the bottom of the view.

Then, if I were to create a view where each budget item had a Debit and Credit amount, how would I be able to create a final column with a running total?

What I need assistance with here is the methodology around this. I am starting to think like a programmer, but I still struggle in places to get my mind to bend around certain problems.

If I could ask the coders out there to help me with methodology, I would be indebted.
Thanks in advance.

#622997

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Pamela

It sounds like you want to do this yourself, and just need a little help.

Let me share the code for a couple of shortcodes with you.

First, the shortcode I use for performing calculations with Types fields:

/**
 * Add custom shortcode to perform calculations with Types fields
 * 
 * attribute 'round' for decimal places, defaults to zero
 */
add_shortcode('calculate', function( $atts = [], $content=null ) {

	$atts = shortcode_atts([ 'round' => 0 ], $atts );

	$content = strip_tags( wpv_do_shortcode($content) );
	$content = eval("return $content;");
	return round($content, $atts['round']);
});

Next, here is a shortcode I use for reporting the iterations of a View:

/**
 * Add loop-iteration shortcode
 * 
 * Attributes
 * 'n' (optional) : test for nth iteration
 */
add_shortcode( 'loop-iteration', function( $atts ){

	global $loop_n;

	if ( !isset( $loop_n ) ) {
		$loop_n = 0;
		return $loop_n;
	}

	$loop_n++;

	if ( !isset( $atts['n'] ) ) {
		// no nth parameter, just return the current index
		return $loop_n;
	}
	else {
		$remainder = ( $loop_n + 1 ) % $atts['n'];
		return ( $remainder == 0 );
	}

});

Without 'n' parameter, the shortcode will simply output the current iteration.

With 'n' parameter it can be used with a conditional shortcode to only show some content for every nth iteration of the loop, like so:

[php]
[wpv-conditional if="( '[loop-iteration n='3']' eq '1' )"]
  <p>3rd item in loop</p>
[/wpv-conditional]

Now, with this second shortcode you can see how you can create global variables which are remembered until the page is reloaded.

So it is an example of how you can create a global variable for the running total of credits, another for the running total of debits, and you can perform a calculation using both to output the net value.

That should give you some ideas, let me know if you get stuck.

#623229

Nigel,

This is extremely helpful and I am very, very grateful.

I would like to experiment with this and, while I do, would it be possible to leave this thread open?

Thanks again!

#623266

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure thing Pamela, let me mark this is as requiring feedback from you, which I think gives you about a week before you have to make a reply of some kind.

#623287

Hi Nigel.

Turns out I'm going to need more than methodology here.

I've implemented the above, but not sure how I would define or create a variable within a view, given the 'index' or iteration provided by the shortcode. I'm sure you meant I should use that shortcode as an example on how to extract certain information from fields as the view renders, but I probably should have mentioned that, while I've proven in the past to do some very complex things with Toolset, I'm not fluent in PHP (as much as I'm trying to be).

I have taken on two huge projects now based solely on my confidence in Toolset. I hope I can pick your brain a bit in the coming weeks.

#623343

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2018-03-08 at 14.51.24.png

Hi Pamela

I don't know what your setup is, so I created a small test site that has a CPT of invoice with numeric custom fields for credit and debit.

I created a few sample posts, with a credit amount, a debit amount, or both.

I created a View with a table layout that outputs these and also the running total using a balance shortcode.

The template part of the View output looks like this:

<td>[wpv-post-title]</td>
<td>[types field='credit'][/types]</td>
<td>[types field='debit'][/types]</td>
<td>[balance credit="[types field='credit'][/types]" debit="[types field='debit'][/types]"]</td>

And the code to register the balance shortcode looks like this:

/**
 * Running balance shortcode
 */
add_shortcode( 'balance', function( $atts ){

	// Initialise running total
	static $running = 0;

	// Set default values of zero for credit and debit
	$atts = shortcode_atts( array(
		'credit'	=>	0,
		'debit'		=>	0 
		),
		$atts );

	// Update running total
	$running = $running + ( $atts['credit'] - $atts['debit'] );

	return $running;
});

You can see the results in the screenshot.

#623566

That's it! I'm sending myself to on some programming courses.

This was so simple. Thank you.

Leaving thread open for a while.

#623605

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure, okay, but let me just mark it as waiting for feedback from you so it's not sitting in my queue.