This topic is locked

How to add and write other field amounts to a single field

11/16/2008 8:05:49 PM
PHPRunner General questions
bbarker author

Is this page still accurate when coding with PHPR Version 5?

http://www.xlinesoft.com/articles/amount_to_single_field.htm
Especially the part that describes "HowTo: Write other fields amount to a single field":

$values["Tax"] = 0.01$values["Price"] + 2$values["Horsepower"];
---I''m struggling with the simple concept of how to get two decimal variables added together and then saved to a blank third variable.
EXAMPLE:

$values["Balance"]= $values["amountreceived"] - $values["amountrefunded"];

C
chaintm 11/16/2008

your example would work as well, you got the idea, the decimal really comes down to what you have it set on in your database , using math here for math such as $value[cost]*.0675 will ofcourse multiply my cost by .0675 and give the return decimal on whatever I set the database decimal at. Most are set to 2.
---I''m struggling with the simple concept of how to get two decimal variables added together and then saved to a blank third variable.
Not sure what you mean here, because $values["Balance"]= would be your total and the math beyond is the caculation just add another $value and + it. Unless again you didn't set a decimal value in your database, if you didn't you will need to do so in the datasource tables then modify table and add a scale of 2 to the $value[Whatevergoeshere].


$values["Balance"]= $values["amountreceived"] + $values["amountrefunded"] + $value["anothervalue"]*1;

bbarker author 11/16/2008

---I''m struggling with the simple concept of how to get two decimal variables added together and then saved to a blank third variable.
Not sure what you mean here, because $values["Balance"]= would be your total and the math beyond is the caculation just add another $value and + it. Unless again you didn't set a decimal value in your database, if you didn't you will need to do so in the datasource tables then modify table and add a scale of 2 to the $value[Whatevergoeshere].
$values["Balance"]= $values["amountreceived"] + $values["amountrefunded"] + $value["anothervalue"]*1;

[/quote]

--------------------------

Thanks for the response.

Let me try to be a little clearer.
I have THREE fields. ALL are defined in MySQL as Decimal (10,2).

In PHPR, I mark them as "currency".
HERE ARE THE FIELDS:

amountreceived is one field.

amountrefunded is a second field.
What I want to to is subtract the second field (refunded) from the first (received)

and then save the result to a THIRD field (Balance).
I said that it was really simple... But when I post the code in as an EVENT, it nevers works.

C
chaintm 11/17/2008

you need to define...
[codebox]$str = "select amountreceived,amountrefunded from YOURTABLE where YOURTABLEKEY='".$values["YOURTABLEKEY"]."'";

$rs = CustomQuery($str);

$data = db_fetch_array($rs);

$values ['amountreceived'] = $data['amountreceived'];

$values["Balance"]= $values['amountreceived'] - $values['amountrefunded']1; [/codebox]
YOURTABLEKEY= usually u use a table key to referance a table for manipulation,

but whatever value you are using on the page to referance as the tracking of that page is

what you want to use.. for me for instances it is ItemID and my table is items...

but whatever yours is put it there. mine looks like this...
[codebox]$str = "select ItemRentalPrice,ItemThumb from items where ItemID='".$values["ItemID"]."'";

$rs = CustomQuery($str);

$data = db_fetch_array($rs);

$values ['ItemRentalPrice'] = $data['ItemRentalPrice'];

$values ['ItemThumb'] = $data['ItemThumb'];

$values["Sub_total"] = $values["Qty_order"]
$values["ItemRentalPrice"]*1;

$_SESSION["ItemThumb"] = $data["ItemThumb"];

$_SESSION["ItemRentalPrice"] = $data["ItemRentalPrice"];

[/codebox]
My math was simple, in this particular code, all I am doing is coping the current Sub_total to my

database by multiplying my amount quantity and itemrentalprice to get my total. I have to call

those pre-defined values for itemrentalprice and the qty_order from my database, the quantity

is done by the user on the fly. The other values you see actually are for displaying on the current

page and also to carry data along with the Sub_total to the new page. Also make sure you watch

for your " and ' and get the right.. if you need real help this is a great checker...
http://www.meandeviation.com/tutorials/lea...p-syntax-check/
the file you will check for say events is usually
D:\vhosts\mj4u\output\include\order_items_events.php
so look for the events.php page u want to check, that checker only checks your php syntex, but it

is a HUGE help in defining.. Also a great idea is if you do get an error is to save that page so you

can in the future look up mysql errors by line sense it opens that up nicly in a very easy to read

numbering and you can just look down the list for the error and see what line the mysql error is

coming from on the code itself.

C
chaintm 11/17/2008

oh I should mention with that all done in the before record, I then finalize in the after record event the following..
[codebox]// This function is used to get sub_total of items passed to the details order_items page.

global $conn,$strTableName;

$str = "select sum(Sub_total) from order_items where Order_num =".$_SESSION[$strTableName."_masterkey1"];

$rs = db_query($str,$conn);

$data = db_fetch_numarray($rs);
$strUpdate = "update order_sales set Order_sub_total =".$data[0]." where order_num =".$_SESSION[$strTableName."_masterkey1"];

db_exec($strUpdate,$conn);
//** Redirect to another page ****

// direct page back to order_items_list page with masterkey being held.

$keys["Order_num"]= $_SESSION[$strTableName."_masterkey1"];

header("Location: order_items_list.php?mastertable=order_sales&masterkey1=".$keys["Order_num"]);
exit();[/codebox]
so that should kinda help you in understanding what I do and how it can help you, if you want the full code let me know I finally got it all working <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=35151&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
P.S. I use everything in decimal setting, currency can work I would assume, but seems it might conflict with something, don't know you could

always try setting to decimal, your call.

bbarker author 11/17/2008

Almost there ---
I used your code with only minor changes, but I receive this ERROR:

Undefined variable: values
[codebox]global $strTableName,$conn;
if ($_SESSION[$strTableName."_masterkey1"])
{
$str = "select amountreceived,amountrefunded from financial where ID ='".$values["ID"]."'";
$rs = CustomQuery($str);
$data = db_fetch_array($rs);
$values['amountreceived'] = $data['amountreceived'];
$values['Balance']= $values['amountreceived'] - $values['amountrefunded'];
db_exec($strupdate,$conn);

}

[/codebox]

C
chaintm 11/18/2008

your value total (balance) is tagged wrong... Use " not ', if that is not it let me know and post full code.
$values["Balance"]

T
thesofa 11/18/2008

just wondering if you need to store the balance in the table, usual practice is never to store a value that you can calculate on the fly?