2009-02-23 05:19:24

gomedia
Plugin Author
omicron
Real name: Adi
Website

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.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 (2010-07-21 00:39:41)

Offline

 

2009-02-23 08:46:42

Bloke
Developer
omega
Real name: Stef Dawson
From: Leeds, UK
Known languages: Engl(ish)
Website

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.

Txp Builders – finely-crafted code, design and Txp

Offline

 

2009-03-05 18:37:28

geoff777
Plugin Author
thêta
Real name: Geoff Forster
From: Benarrabá Andalucía Spain
Known languages: en > es
Website

Re: adi_calc – Calculator for TXP variables

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

 

2009-03-06 01:06:10

gomedia
Plugin Author
omicron
Real name: Adi
Website

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

 

2009-03-17 05:47:46

maniqui
Moderator
omega
Real name: Julián
From: Buenos Aires, Argentina
Known languages: es,en

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?


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

 

2009-03-17 06:22:29

gomedia
Plugin Author
omicron
Real name: Adi
Website

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 (or set, or base) 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 new txp:variable value)?

What do you think, Adi?

As always, no unreasonable requests refused … I’ll look into it.

Offline

 

2009-03-20 01:56:42

gomedia
Plugin Author
omicron
Real name: Adi
Website

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 variable
  • display to display result of the calculation

Offline

 

2009-04-29 09:36:48

pieman
Member
lambda
Real name: Stuart Charlton
From: Bristol, UK
Known languages: Mackem
Website

Re: adi_calc – Calculator for TXP variables

Really useful, thanks

Offline

 

2009-08-25 00:26:47

maniqui
Moderator
omega
Real name: Julián
From: Buenos Aires, Argentina
Known languages: es,en

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.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

 

2009-08-25 12:10:47

gomedia
Plugin Author
omicron
Real name: Adi
Website

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

 

Powered by FluxBB