Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2009-02-23 05:19:24
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
adi_calc – Calculator for TXP variables
Summary
adi_calc does calculations using TXP variables.
See the help supplied with the plugin, or have a look online.
Examples
Single tag mode
<txp:variable name="test" value="2" />
set a variable called test to 2
<txp:adi_calc name="test" add="5" />
adds 5 to test (result = 7)
The setting of the variable and the first calculation can be combined using the value
attribute:
<txp:adi_calc name="test" value="2" add="5" />
sets test to 2 and adds 5 to it (result = 7)
Use display="1"
to display the result of the calculation:
There are <txp:adi_calc name="total" value="60" multiply="24" display="1" /> minutes in a day.
To store the result of a calculation in a different TXP variable:
<txp:adi_calc name="source" add="5" result="destination" />
adds 5 to the value of source & stores the result in destination, source variable left unchanged
Container tag mode
<txp:adi_calc name="x" add="2">4</txp:adi_calc>
contents (i.e. 4) override initial value of supplied variable x, but the result will be stored in x
<txp:adi_calc display="1" add="2" result="y">
<txp:variable name="x"/>
</txp:adi_calc>
initial value taken from variable x, 2 added, result stored in y and displayed, x is unchanged
Floating point calculations
<txp:adi_calc name="float" value="1" div="3" precision="" />
divides 1 by 3 (result = 0.33333333333)
<txp:adi_calc name="float" value="2" div="3" precision="2" />
2 divided by 3, two decimal places (result = 0.67)
Without variables!
There are <txp:adi_calc value="60" multiply="24" />
minutes in a day.
<txp:adi_calc add="4">20</txp:adi_calc>
is two dozen.
Using reset & reset counter
Consider the following:
<txp:variable name="counter" value="0" />
<txp:variable name="reset_counter" value="0" />
counter = 0, reset_counter = 0
<txp:adi_calc name="counter" add="1" reset="4" reset_name="reset_counter" />
counter = 1, reset_counter = 0
if the above adi_calc tag is repeated a further 6 times, then the sequence of values will be as follows:
counter = 2, reset_counter = 0
counter = 3, reset_counter = 0
counter = 0, reset_counter = 1
counter = 1, reset_counter = 1
counter = 2, reset_counter = 1
counter = 3, reset_counter = 1
Each time counter reaches the value of 4, it is reset to zero and the reset counter is incremented. This can be useful when counting rows & columns. In the above example, reset_counter would be row and counter would be column.
Version History
- 1.2
- TXP 4.6 tag registration
- 1.1
- tidied up integer/floating point modes, default now strictly integer, floating point switched on with ‘precision’ (thanks Bloke)
- ‘div’ can be specified as ‘divide’ (improve code readability)
- 1.0
- new attribute: ‘precision’ to switch into floating point mode
- new attribute: ‘result’ variable set to result of calculation, original variable left unchanged
- new attribute: ‘reset’ to automatically reset value to zero
- new attribute: ‘reset_name’ to count the number of resets
- enhancement: container mode can be used to set the initial value
- ‘name’ attribute no longer mandatory
- no longer picky about lack of initial value – if all else fails use zero
- 0.3
- fix: error if adi_calc is the first person to set a TXP variable (thanks maniqui)
- new attribute: ‘ceiling’ to always round up the result of integer division (for aswihart & maniqui)
- 0.2
- new attribute: ‘value’ to explicitly set an initial value to variable (for maniqui)
- new attribute: ‘display’ to display result of the calculation (for maniqui)
- 0.1
- initial release
Requirements
Tested in:
- Textpattern 4.0.7+
adi_calc can be installed in pre-4.0.7 versions of Textpattern but obviously the creation of TXP variables won’t work!
Download & Installation
Download adi_calc and install the plugin in the usual way.
Bugs & Limitations
- adi_calc assumes that supplied TXP variables & values are numeric, but no checks are made to verify this.
Let me know if you find any problems.
Feedback
Comments are most welcome.
Last edited by gomedia (2015-06-15 01:14:06)
Offline
Re: adi_calc – Calculator for TXP variables
Nice nice nice nice nice, this is gonna be uber-useful. Just what the doctor ordered, thanks.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: adi_calc – Calculator for TXP variables
Thanks Adi another great plugin.
I added concatenation prefix and suffix (easy if I did it!)
Geoff
There are 10 types of people in the world: those who understand binary, and those who don’t.
Offline
#4 2009-03-06 01:06:10
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
Geoff, well done – glad you could adapt it.
I bet the visionary wet has a “knowing” smile on his face when he reads what you’ve done!
Offline
Re: adi_calc – Calculator for TXP variables
Great plugin. I’m checking if I can replace some “complex” (not really) math I’m doing with rvm_counter (a modified version I got from ruud) by using just adi_calc.
So far, it has been helpful in some places.
And now, two feature requests:
1) from the help:
if the supplied TXP variable is not set, then adi_calc sets it to zero before the calculation is performed.
Would it be possible to add an initial
(or set
, or base
) attribute, to set the initial variable value on the fly and performing the operation at the same time?
So, this:
<txp:variable name="caption_width" value="{width}" />
<txp:adi_calc name="caption_width" subtract="30" />
can become just this:
<txp:adi_calc name="caption_width" subtract="30" initial="{width}" />
2) Would it be possible to make adi_calc to “echo” (new attribute echo
) the resulting value (ie. the new txp:variable
value)?
If so, then this:
<txp:variable name="caption_width" value="{width}" />
<txp:adi_calc name="caption_width" subtract="30" />
<txp:variable name="caption_width" />
will become just this:
<txp:variable name="caption_width" value="{width}" />
<txp:adi_calc name="caption_width" subtract="30" echo="1" />
So, if both requested features (1 and 2) could be implemented, then this:
<txp:variable name="caption_width" value="{width}" />
<txp:adi_calc name="caption_width" subtract="30" />
<txp:variable name="caption_width" />
could be written just like this:
<txp:adi_calc name="caption_width" subtract="30" initial="{width}" echo="1" />
What do you think, Adi?
Offline
#6 2009-03-17 06:22:29
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
maniqui wrote:
Great plugin. I’m checking if I can replace some “complex” (not really) math I’m doing with rvm_counter (a modified version I got from ruud) by using just adi_calc.
Thanks – glad it’s been of some use.
And now, two feature requests:
1) Would it be possible to add an
initial
(orset
, orbase
) attribute, to set the initial variable value on the fly and performing the operation at the same time?2) Would it be possible to make adi_calc to “echo” (new attribute
echo
) the resulting value (ie. the newtxp:variable
value)?What do you think, Adi?
As always, no unreasonable requests refused … I’ll look into it.
Offline
#7 2009-03-20 01:56:42
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
Version 0.2 available for download.
This has a couple of new attributes, as per maniqui’s request:
value
to explicitly set an initial value to variabledisplay
to display result of the calculation
Offline
Re: adi_calc – Calculator for TXP variables
Really useful, thanks
Offline
Re: adi_calc – Calculator for TXP variables
Hi Adi,
I’m getting this error:
Tag error: <txp:adi_calc name=“clip” add=“1” display=“1” /> -> Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object on line 68
The code (as the error message displays):
<txp:adi_calc name="clip" add="1" display="1" />
As you can see, I’m trying to create the variable, sum 1 and display it on one line. I’m almost sure this worked for me on another project.
The error goes away if I initialize the variable and set it to a value before using adi_calc, like this:
<txp:variable name="clip" value="0" />
<txp:adi_calc name="clip" add="1" display="1" />
I’m on TXP 4.2.0, if that helps.
Thanks.
Offline
#10 2009-08-25 12:10:47
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
Hi Julián,
I can’t reproduce this on my system. What version of PHP are you using?
Just a hunch, and it sounds bizarre, but what happens if you initialise some other variable before using adi_calc, e.g.
bc.<txp:variable name=“notclip” value=“0” />
<txp:adi_calc name=“clip” add=“1” display=“1” />
Offline
Re: adi_calc – Calculator for TXP variables
hi Adi,
thanks for replying.
No, it doesn’t happen if I initialize some other variable :)
I’m on PHP 5.2.6-3ubuntu4.2
Offline
#12 2009-08-26 00:51:15
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
maniqui wrote:
No, it doesn’t happen if I initialize some other variable :)
Good, I’ll produce a fix but in the meantime please use the “initialiase a variable” workaround.
Offline