How to use $rsold and $rsnew in Grid_Updated server event

Tips submitted by PHPMaker users
Post Reply
mobhar
User
Posts: 11660

How to use $rsold and $rsnew in Grid_Updated server event

Post by mobhar »

The "Grid_Updated" server event is used with "Grid-Edit" for a table, and "Master/Detail-Edit" for a detail table.

This event will be called after updating all records. The argument of the event ($rsold and $rsnew) are array of records before and after update (retrieved from database).

For example: You can use this event to insert all the records in "Grid-Edit" mode to another table that has same structure with the current table.

To implement that example, then let's assume the original table has the same structure with the "target_table", and for simplicity, this table contains only two fields. Both "FieldOne" and "FieldTwo" is a string/varchar field type. "FieldOne" is a primary key, therefore we will get the value from the old recordset. The value in "FieldTwo" is changed in this "Grid-Edit", therefore we will get the value from the new recordset. Of course, you may enhance this example by adding another new fields.

So here is the code how you can expose the values from both $rsold and $rsnew and process them to insert to another table.

$i = 0;
foreach ($rsnew as $row_new) {
$i++;
$j = 0;
foreach ($rsold as $row_old) {
$j++;
if ($i == $j) {
ew_Execute("INSERT INTO target_table (FieldOne, FieldTwo) VALUES ('" . $row_old["FieldOne"] . "', '" . $row_new["FieldTwo"] . "')");
}
}
}

By using that code, then all the records in Grid-Edit will be inserted into "target_table".

Hopefully this will be useful for those who need the example of how to use $rsold and $rsnew in "Grid_Updated" server event.

Happy coding, everyone! :-)


anil
User
Posts: 24

Post by anil »

Well...Working fine


rocarona
User
Posts: 1

Post by rocarona »

thanks for the tips.

regarding master/detail setup, is it possible to automatically update field value as a result from calculation between 2 other field on master table which one of them is total sum from detail table?

for example:

master

id (int)
jams (varchar)
nilai (decimal)
cicilan (decimal) <===== total sum from detail.total
sisa (decimal) <==== should be 'nilai' - 'cicilan'

detail

id (int)
item (varchar)
qty (double)
cicilan (decimal)
total (decimal) <===== should be 'qty' - 'cicilan'

Thanks for your help...


kirondedshem
User
Posts: 642

Post by kirondedshem »

to get a value as a result of calculating from detail and put it into master table in master/detail add/edit form,
phpmaker has no automated way around this yet So
the way I currently do it is using jquery where I can find all detail elements, get thier values and sum them up the put result into a filed in master form. I put this logic in a fnction which I can call onchange from any field I want
You can look at these two topics to get an idea on how to:

"Total of checked items (Selected Items) from list" on viewtopic.php?f=4&t=40703
AND
"Total Qty" placement under detail table on viewtopic.php?f=4&t=40925

Just look at the implimentation and change it to your own or make your own similar approach


Post Reply