I’m muddling through a straightforward scenario. Thanks in advance for any feedback. I’m very familiar with ModX, but PHP isn’t in my wheelhouse, hence a learning opportunity:
I seek an average of values assigned to the template variable dayRank. I’m incrementally working my way to this; currently, I have a snippet drVal that captures the dayRank value.
<?php
$dr_value= $dr;
return($dr_value);
This is very simple, obviously, and outputs the dayRank value when called from the resource template to which the dayRank TV has been assigned.
I next seek to retrieve and output dayRank values to a separate resource, on which I have:
I included [[+idx]] and [[+pagetitle]] simply to know that getResources was retrieving (the correct) resources, which it is. dayRate is not, however.
As mentioned, ultimately I need dayRank average – as mentioned, at this stage I simply seek dayRank for each resource returned with the getResources call.
The getResources call will return everything as one big string, which you would then have to parse.
Since it looks like you don’t really need the pagetitle or ID, you might consider this approach that avoids the overhead of getResources.
There are faster ways to do this, but this one is easier to understand:
[[!AverageDayRate]]
/* AverageDayRate snippet -- displays the average day rate. */
/* Get all children of resource 6 */
$docs = $modx->getCollection('modResource', array('parent' => 6));
/* get number of documents */
$count = count($docs);
/* initialize total day rate at 0 */
$totalDayRate = 0;
foreach ($docs as $doc) {
$dayRate = $doc->getTVValue('dayRate');
$totalDayRate += $dayRate;
}
/* return average DayRate */
return $totalDayRate / $count;