Calculation in a form

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

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: [Jason] Calculation in a form

Thanks Jason,

It's going to take me a while to get my head around this one . If you have any examples that I could follow and build on that would help a lot.

Thanks,

Jerry
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 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/