This question has been answered by dashukin. See the first response.
<?php
$doc = $modx->getObject('modResource', 15));
$values = array();
$output = '';
if ($doc) {
$children = $doc->getMany('Children');
foreach ($children as $child) {
$tvValue = $child->getTVValue('engine_type');
if (in_array($tvValue, array_keys($values))) {
$values[$tvValue] = $values[$tvValue] + 1;
} else {
$values[$tvValue] = 1;
}
}
foreach ($values as $key => $value) {
$output .= '<br />Type: ' . $key . ' (' . $value . ')';
}
}
return $output;$children = $doc->getMany('Children');// First fetch all the children of the current resource
<?php
function process ($children, &$values) {
foreach ($children as $child) {
$tvValue = $child->getTVValue('engine_type');
if (in_array($tvValue, array_keys($values))) {
$values[$tvValue] = $values[$tvValue] + 1;
} else {
$values[$tvValue] = 1;
}
$moreDocs = $child->getMany('Children');
if (! empty($moreDocs)) {
process($moreDocs);
}
}
}
$doc = $modx->getObject('modResource', 15));
$values = array();
$output = '';
if ($doc) {
$children = $doc->getMany('Children');
process($children, $values);
foreach ($values as $key => $value) {
if (!empty($key)) {
$output .= '<br />Type: ' . $key . ' (' . $value . ')';
}
}
}
return $output;$doc = $modx->getObject('modResource', 15));
$parents = '216,215,408,499,214';
$doc = $modx->getCollection('modResource', $parents);
$values = array();
$output = '';
if ($doc) {
$children = $doc->getMany('Children');
foreach ($children as $child) {
$tvValue = $child->getTVValue('engine_type');
if (in_array($tvValue, array_keys($values))) {
$values[$tvValue] = $values[$tvValue] + 1;
} else {
$values[$tvValue] = 1;
}
}
foreach ($values as $key => $value) {
$output .= '<br />Type: ' . $key . ' (' . $value . ')';
}
}
return $output;
[[SnippetName? &parentList=`216,215,408,499,214`]]
$parentList = $modx->getOption('parentList', $scriptProperties, '');
$parents = empty($parentList)? array() : explode(',', $parentList);
$values = array();
$output = '';
$c = $modx->newQuery('modResource');
$c->where(array('id:IN' => $parents));
$docs = $modx->getCollection('modResource', $c);
if (! empty($docs)) {
foreach($docs as $doc) {
$children = $doc->getMany('Children');
foreach ($children as $child) {
$tvValue = $child->getTVValue('engine_type');
if (in_array($tvValue, array_keys($values))) {
$values[$tvValue] = $values[$tvValue] + 1;
} else {
$values[$tvValue] = 1;
}
}
}
natsort($values);
foreach ($values as $key => $value) {
$output .= '<br />Type: ' . $key . ' (' . $value . ')';
}
}
return $output;
$engineTypeTVID = 100500; // your TV id
$carTemplateId = 78; // your car template id
$startParentId = 10;
$childrenIds = $modx->getChildIds($startParentId, 3, array('context' => 'web'));
$q = $modx->newQuery('modResource');
$q->leftJoin('modTemplateVarResource', 'eType', array('modResource.id = eType.contentid', 'eType.tmplvarid = ' . $engineTypeTVID ));
$q->select(array('count(modResource.id)', 'eType.value as eType'));
$q->where(array(
'modResource.id:IN' => $childrenIds,
'modResource.template' => $carTemplateId
));
$q->groupby('eType');
if ($q->prepare() && $q->stmt->execute()) {
$rs = $q->stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>';
print_r($rs);
echo '</pre>';
}
dashukin's snippet would be significantly faster than mine.Hi, BobRay!