field calculation

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
made

field calculation

Post by made »

hi, i just bought phpmaker (the reason is because i have little knowledge in php coding) and phpmaker saved the day :)
i need some help right now

i have table "store" with 6 fields:

  1. Item_name1 : varchar
  2. Item_name2 : varchar
  3. item_name3 : vachar
  4. price : integer
  5. quantity: integer
  6. Total : integer

data:

  1. Item_name1 : AA
  2. Item_name2 : BB
  3. item_name3 : AABB (question 1)
  4. price : 120
  5. quantity: 2
  6. Total : 240 (question 2)

Question:

  1. How to make field item_name3 autofill/autogenerate with combined Item_name1&Item_name2 on Input/Edit Page?
  2. How to make field Total autofill/autogenerate with the result of price x quantity on Input/Edit Page?

please someone willing share some knowledge
thanks in advanced


Webmaster
User
Posts: 9425

Post by Webmaster »

Unselect item_name3 and Total from the Add/Edit page, then use Row_Inserting/Updating event (see Server Events and Client Scripts in the help file) to set them, e.g.

$rsnew["item_name3"] = $rsnew["item_name1"] . $rsnew["item_name2"];
$rsnew["Total"] = floatval($rsnew["price"]) * intval($rsnew["qunatity"]);


made

Post by made »

how to make it auto calculate before insert (client side)?


Webmaster
User
Posts: 9425

Post by Webmaster »

Then you need to add onchange client side event, you can use Client Script or Startup Script, see examples in Server Events and Client Scripts in the help file.


made

Post by made »

for this example: $rsnew["item_name3"] = $rsnew["item_name1"] . $rsnew["item_name2"];

if i change item_name1=integer with autoincrement
the script won't work, please help me how to make it work


Webmaster
User
Posts: 9425

Post by Webmaster »

You don't know the new autoincrement ID yet before insert. You should update it after insert using Row_Inserted event.


made

Post by made »

I put the code ($rsnew["item_name3"] = $rsnew["item_name1"] . $rsnew["item_name2"];)
on Row_Inserted event but nothing happen


Webmaster
User
Posts: 9425

Post by Webmaster »

Row_Inserted is fired AFTER insert, you cannot update a value by just setting the value in $rsnew. You need to execute your own UPDATE statement, e.g.

ew_Execute("UPDATE YourTable SET YourField = YourValue WHERE YourAutoIncrementField = " . $rsnew[" YourAutoIncrementField"]);

To concat by SQL, read:
http://dev.mysql.com/doc/refman/5.0/en/ ... ion_concat


chimangabee
User
Posts: 5

Post by chimangabee »

Webmaster wrote:
Unselect item_name3 and Total from the Add/Edit page, then use
Row_Inserting/Updating event (see Server Events and Client Scripts in the
help file) to set them, e.g.

$rsnew["item_name3"] = $rsnew["item_name1"] .
$rsnew["item_name2"];
$rsnew["Total"] = floatval($rsnew["price"]) *
intval($rsnew["qunatity"]);

My Question is, will the Total be displayed on the confirmation page also? if not how do i get it to be displayed when i dont want it to be shown on the add page also


mobhar
User
Posts: 11660

Post by mobhar »

That server event code above will not display the Total value on the confirmation page. Since that is server event code, then it will be only used for calculating process before the record is added/updated into database.


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

This have been discussed many times before with real examples f.ex http://www.hkvforums.com/viewtopic.php?f=4&t=40985


Post Reply