This topic is locked

Calculated Fields - I'm a newbie

6/25/2006 10:53:43 PM
PHPRunner General questions
D
dilbertbert author

Help!
I'm a newbie, no PHP experience (but I want to learn)
What is the best way to put data into fields that are calculations of other fields?
I want to take a price multiplied by a quantity to get an extended price for a detail record on an order_detail table. I then want to get a total of all of the extended prices in the detail records for an order, take that total and put it into a field in the master order for that correspdonding order number.
I don't know if this is a sql query (I am using mysql) or php code on an even.
I'll take all the advice I can get.
Thanks in advance!!

Alexey admin 6/26/2006

Hi,
I recommend using calculated fields for extended price calculation and Events for Master table updating.
Enter this query on Edit SQL query tab in PHPRunner:

select

...

*pricequantity as price_ext**

from `order details`



to calculate extended price column.
Put this code to After record added/updated and After Mass Delete events

if($_SESSION[$strTableName."_masterkey"])

{

global $conn;

$rs=db_query("select sum(price*quantity) from `order details` where orderid=".$_SESSION[$strTableName."_masterkey"],$conn);

$data=db_fetch_numarray($rs);

$total=$data[0];

db_exec("update orders set total=".$total." where id=".$_SESSION[$strTableName."_masterkey"],$conn);

}


The code above will work if you have master-details relationships defined.

D
dilbertbert author 6/26/2006

Thanks for the reply, I will give this a try!!