average values column

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
facos79
Veteran Member
Posts: 119
Joined: 2014-10-29 12:31

average values column

Post by facos79 » 2023-08-23 12:27

Good morning everyone,
I would need to display not only the sum of the values in the column but also their average.
How can I do?
Can anyone help me with a sample code?
Can hooks be used so I don't have to manually edit the page each time?
Thanks again.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1231
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: average values column

Post by onoehring » 2023-08-27 08:02

Hi,

if you need average displayed, you will need to change the layout of the page.
You can do this either by using Javascript (someone else on the forum can probably help) and inject "something" (see below) at the position or change the base layout file of the page in the /templates/tablename-dv or just add the average to the footer function of your file.
Something like this should work:

Code: Select all

function avgDisplay(&$options) {
    $sql_from = $options->QueryFrom;
    $sql_where = $options->QueryWhere;
    $sql = "SELECT AVG(YOURFIELD) AS AverageFromYOURFIELD FROM (SELECT * FROM (" . $sql_from . ") WHERE (" . $sql_where ."));"
    $avgCalculated = sqlValue($sql);
    return $avgCalculated;
}

Using the code above (not tested), you should be able to display your average by simply calling

Code: Select all

echo avgDisplay($options);
from anywhere within your page code.

If this does not work, you may need to calculate the average directly in the _ini function and save the calculated result to a variable that you can reuse later (session var).

Olaf

facos79
Veteran Member
Posts: 119
Joined: 2014-10-29 12:31

Re: average values column

Post by facos79 » 2023-08-31 15:51

Thanks for the reply.
Do you know where the script that makes the Sum work is? Because I would like to place them close together


Post Reply