This topic is locked
[SOLVED]

Before Record Update simple calculations issue displaying

12/30/2021 4:03:05 AM
PHPRunner General questions
D
Dynamiccomp author

I have the following calculations in the before record update event:

if ($values['Division'] == "Novice") {

$values['Marks'] = $values['Score1'] + $values['Score2'] + $values['Score3'] + $values['Score4'] + $values['Score5'] + $values['Score6'] + $values['Score7'] +
$values['Score8'] + $values['Score9'] + $values['Score10'] + $values['Score11'] + $values['Score12'] + $values['Score13'] + $values['Score14'] + $values['Score15'] +
$values['Score16'] + $values['Score17'] + $values['Score18'] + $values['Score19'] + $values['Score20'] + $values['Score21'];
$values['Net Results'] = $values['Marks'] - $values['Penalties'];
$values['Result Total'] = ($values['Net Results'] / 210) * 100;
$values['Total'] = 100 - $values['Result Total'];
}
elseif ($values['Division'] == "Training") { ...}

the problem I am having and I'm not sure why, probably something that I am doing incorrectly, it takes 4 times to edit and save the record for the equations to all populate.
The first field to populate is the Marks, than after the 2nd is the Net Results, the 3rd is the Result Total, and sometimes the Total, but other times it takes a 4th save to populate the total.

Now i have played around and tried adding these calculations directly into the SQL query, but that had the same result. i also tried breaking up the code into pieces into:

  • Before record updated
  • After record updated
  • process record values

but this did not change the outcome.

What i am looking for in to have all of these calculations performed in a single click of the "save" button, but not sure how to accomplish this exactly.

Does anyone have some pointers to lead me in the right direction?

K
kohle 12/30/2021

Hi,

do I understand correctly,
you open an edit page of a record and click on save and
in the "before record updated" event you have your code
which should update fields
$values['Marks'] , $values['Net Results'] ....
in the same record ?

Other question, the update depends on
$values['Division']

does this value change too ?
In "before record updated" event you have $values and $oldvalues

$values - array of values to be written to the database. To access specific field value use $values["FieldName"]
$oldvalues - array with existing field values. To access specific column value use

you can use : echo "Division:" $values['Division'];
in your script to see the contents.
If you run your project you will get at the top a server error. Click on this and you see the value of the echo.

rg
J

D
Dynamiccomp author 12/30/2021

Thanks, I may have not explained myself correctly.

So what happens, is I have a "registration table" where the rider information is entered, which then also updates the "scoring" table. Then when ready to enter scores we open up the "scoring" table > click on edit, for that record, and then enter all of the scores in each "ScoreXX" field.

So what i am saying then happens is when i click on "save" and taken back to the list, there is only data in the "Marks" field. So then if i click on edit, and then on "save" again it would then populate the "Net Results" and so on. Shouldn't it perform all of those calculations in a single "save" click? Or am I doing something wrong?

The Division field doesn't get updated, it's only there because every division has a different number of scores to calculate.

I just do not understand why it takes so many clicks for it to populate/perform all of those calculations, I figured they would all be done sequentially with a single click.

fhumanes 12/31/2021

Hello,

I think the problem can occur by doing algebraic operations with null values.

Initially, a whole are null, when stored they were "0" and so, you are doing the operations little by little.

Before doing the operation, it verifies that the values are not null.

Greetings,
fernando

D
Dynamiccomp author 1/1/2022

How would i fix that issue without storing an inital value of "0"? Since we want all values to be blank before scores are entered.

fhumanes 1/2/2022

Hello,
Take all the values for this function.

function nullZero($value){
return $value !== null ? $value : 0;
}

Greetings,
fernando