[SOLVED] Update Selected - BeforeEdit event - calculations |
4/7/2025 10:04:18 AM |
PHPRunner General questions | |
![]() Just a reminder for future myself and if anybody finds it useful. If we perform calculations in the "Before record updated" event (BeforeEdit), something like this:
This works perfectly in Edit and Inline Edit. But if we in the future include "Update selected" this type of calculations will not work, mostly it will calculate wrong result (total = 0) To make it work in all 3 situations ("Edit", "Inline Edit" & "Update Selected") we need to put this line of code at the beginning of the BeforeEdit event:
or if we don't want to remove keys from the array:
Both are ok. Because in "Update selected" the $values array contains only the changed element (for example $values['discount'] = 20), other elements in $values array are not present so they will be interpreted in these calculations as either empty or 0. This line of code copies all the elements from the $oldvalues array to $values array, replaces old values with new ones from current $values and removes the $keys elements ///////////////////////////////// Let's say that we have this simple table and data: CREATE TABLE `invoice_details` ( INSERT INTO `invoice_details` (`product_id`,`quantity`,`unit_price`,`discount`,`total`) VALUES (1, 25.00, 10.00, 10, 225.00); If we have a page with Add, Edit, Inline Edit, Update Selected And in the "Before record updated" event we have this simple calculation:
Now the user wants to add a discount to more than 1 record. This all works perfectly in "Edit" and in "Inline Edit", but if the user uses the "Update selected" and enters only the discount (or changes any other field) the total calculation is wrong (0) because now in the $values array we only have returned $values['discount'] |
|
![]() |
Davor Geci authorDevClub member 4/7/2025 |
Just to clarify, it's not a PHPRunner error, that's how it works, because that way we get data about the elements that the user entered, and in the case of "Update selected" he only changed some elements, not all. In some situations, we only need those elements that have changed, and when we need all the elements for calculations, we add that one line of code. Davor |
![]() |
Dalkeith 4/11/2025 |
Thank you Davor for the documentation |