Page 1 of 1

average values column

Posted: 2023-08-23 12:27
by facos79
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.

Re: average values column

Posted: 2023-08-27 08:02
by onoehring
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

Re: average values column

Posted: 2023-08-31 15:51
by facos79
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

Re: average values column

Posted: 2023-09-01 11:41
by onoehring
Hi,

sorry, no, I not not know this.

Olaf