Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Calculation in a form

 

 


gkornbluth
Veteran

Nov 28, 2011, 9:13 AM

Post #1 of 4 (229 views)
Shortcut
Calculation in a form Can't Post

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


Jason
Staff / Moderator


Nov 28, 2011, 10:04 AM

Post #2 of 4 (227 views)
Shortcut
Re: [gkornbluth] Calculation in a form [In reply to] Can't Post

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 - Programmer 
interactivetools.com

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


gkornbluth
Veteran

Nov 29, 2011, 2:52 PM

Post #3 of 4 (213 views)
Shortcut
Re: [Jason] Calculation in a form [In reply to] Can't Post

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


Jason
Staff / Moderator


Nov 30, 2011, 10:50 AM

Post #4 of 4 (197 views)
Shortcut
Re: [gkornbluth] Calculation in a form [In reply to] Can't Post

Hi Jerry,

Here is a basic frame you can start from:


Code
<?php 
/*
Plugin Name:
Description:
Version: 1.00
Requires at least: 2.08
*/


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 - Programmer 
interactivetools.com

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