Calculation in a form

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 30, 2011   (RSS)

Hi all,


I did some Googling and I think I'm more confused then when I started. So, before I embark on this journey, I thought I’d ask for some input from you folks.

Has anyone had success with a straight forward approach to complex calculations inside a form using CMSB?

Here’s what I’m trying to do.

A customer has a number of standard sizes (in inches) for the printing of images. (I’m including these in “Standard Width” and “Standard Height” options fields in my form.

There’s also a Price Per Square Foot of print media (in US dollars and cents) that are in another options field.

There are also times when other services get added to the price, like special mounting which add various values to the price per square foot. These would be selected from another options field.

On submitting the form I’d like the Total Print Price to appear in another field.

So the formula would be something like:

((Standard Width - inches X Standard Height - inches )/144) X (Price Per Square Foot + optional factor for mounting) = Total Print Price (formatted for US dollars and cents)

Any ideas appreciated. (Even another approach)

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Calculation in a form

By Jason - November 28, 2011

Hi Jerry,

If the calculation is made after submission of the form, you could do your calculation in a plugin that uses the record_postsave hook.

You can have a "total_print_price" field in your section. It would be good to set this as an admin/system field, with a description that this is an auto-calculated field that should not be changed.

In your plugin function, you can calculate your total price based on your formula, and then update the record, setting total_print_price to your calculated value.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [gkornbluth] Calculation in a form

By Jason - November 30, 2011

Hi Jerry,

Here is a basic frame you can start from:

<?php



addAction('record_postsave', 'pluginName_functionName', null, 4);


function pluginName_functionName($tableName, $isNewRecord, $oldRecord, $recordNum) {

$pluginTableName = "tableThePluginIsFor";

if ($tableName != $pluginTableName) { return; }

$record = mysql_get($pluginTableName, $recordNum);




}

?>

First thing is you'll need to replace pluginName_functionName with the name of the function that is going to be executed after a record is saved. We tend to use the convention of pluginName_functionName to avoid naming collisions.

Next, assign the name of your section to the variable $pluginTableName. This will ensure that the function is only executed when a record is saved in a specific section.

Finally, the next line assigns $record with the value of the record that was just saved.

You will also have access to everything that was in the $_REQUEST array from this function as well.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/