You are not logged in.
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
Requirements
Tested in:
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
Let me know if you find any problems.
Feedback
Comments are most welcome.
Last edited by gomedia (2010-07-21 00:39:41)
Offline
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.
Txp Builders – finely-crafted code, design and Txp
Offline
Thanks Adi another great plugin.
I added concatenation prefix and suffix (easy if I did it!)
Geoff
“Do you think yourself wise? Then there’s a donkey inside your waistcoat”
Offline
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
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:variablevalue)?What do you think, Adi?
As always, no unreasonable requests refused … I’ll look into it.
Offline
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 calculationOffline
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
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