Quote from: lokust at Feb 01, 2009, 12:34 PM
Bunk that code chunk works well though only for the specific item id (contentid). Any idea on how to specify a parent id and then loop through the child documents looking for the TV value ’Leisure" ?
Here’s another approach...
Use $modx->getChildIds() to get the children id’s, then simply use those as the criteria in your query using the SQL IN clause:
<?php
$total = '0';
$parentId = !empty($parentId) ? $parentId : 0;
$childIds = $modx->getChildIds($parentId, 1);
if (!empty($value) && !empty($childIds)) {
$query = "SELECT COUNT(`value`) FROM " . $modx->getFullTableName('site_tmplvar_contentvalues') . " WHERE `tmplvarid` = {$tmplVarId} AND `value` = '{$value}' AND `contentid` IN (" . implode(',', $childIds) . ")";
if ($result = $modx->db->query($query)) {
$total = $modx->db->getValue($result);
}
}
return (string) $total;
?>
NOTE: I added the `tmplvarid` = {$tmplVarId} because this will make the query much more efficient, restricting the number of records that have to be searched for the value you are looking for. You can get the id of your Template Variable from the manager interface (it’s next to the name in parenthesis).
Then just pass a parentId, value, and a tmplVarId in the snippet call to make it configurable...
[!GetTVCount? &parentId=`15` &value=`Leisure` &tmplVarId=`10`!]
This snippet can count the number of direct children that have an actual value recorded for a specific value in a specific TV.