Calculated fields are useful when a value needs to be derived from other fields, such as totals, averages, taxes, or percentages. In PHPRunner and ASPRunner.NET, there are several ways to calculate values depending on whether the result only needs to be displayed, saved to the database, or updated instantly while the user edits a record.
In the examples below, we will use a simple Cars table with fields such as Price, Horsepower, EPACity, EPAHighway, and Tax.
Which method should I use?
In this example, we calculate the average of the EPACity and EPAHighway fields. This approach works best when you need to display a calculated value but do not need to save it in the database.
1. Open your project and proceed to the Edit SQL Query screen.
2. Switch to SQL mode.
3. Add the following expression to the query: ([EPACity] + [EPAHighway])/2 as AvgEpa.
4. Proceed to the Choose fields screen.
5. For the AvgEpa field, clear the following checkboxes: Add, Edit, Inline Add, Inline Edit.
6. Build the project and view the calculated field in the web application.
In this example, the Tax value is calculated from the Price and Horsepower fields and saved to the database whenever a record is added or edited.
1. Open the Before Record Updated event.

2. Add the following code to calculate the Tax value.
3. Apply the same calculation in the Before Record Added event so the Tax value is also calculated for new records.
Sometimes you want users to see a calculated value immediately, before the record is saved. In this example, the Tax field is recalculated whenever the Price or Horsepower value changes.
1. Open your project and proceed to the Events screen.
2. Open the JavaScript OnLoad event.
3. Add the following code:
Now, when a user adds or edits a record, the Tax field is recalculated immediately whenever the Price or Horsepower field changes. The user can see the result before saving the record.
PHPRunner gives you several ways to add calculated values depending on whether you need them for display, storage, or instant feedback on the page. Learn more about PHPRunner.
Applies to: