How to add up content of TVs


I have various sub-documents in a folder. These documents have a TV which each has a number. How can I add up these numerical values?

Do someone have an Idea?

In a snippet either use an SQL query like this

FROM modx_site_tmplvar_contentvalues

or load the values with xPDO ($modx->getCollection('modTemplateVarResource', $query), iterate over the result and add up the values.

Thanks, that is a good starting piont for me.

Here is some more detailed example code (that works only for direct children of the resource):

$parent_id = 1;
$tv_id = 2;
$stmt = $modx->prepare("SELECT SUM(value) FROM modx_site_tmplvar_contentvalues WHERE tmplvarid = ? AND contentid IN (SELECT id FROM modx_site_content WHERE parent = ? AND published = 1 AND deleted = 0)");
$stmt->execute([$tv_id, $parent_id]);
$sum = $stmt->fetchColumn(0);
return $sum;
1 Like

Oh thank you very much. I do try this.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.