In the following , please replace # __ with #__
global $globalcats; $top_cat = 19; // = false; // use false for all items if ($top_cat) { $cats_list = implode(',',$globalcats[$top_cat]->descendantsarray); $join_cats = ' JOIN # __flexicontent_cats_item_relations AS rel'."\n"; $join_cats .= ' ON rel.itemid=i.id '."\n"; $where_cats = ' AND rel.catid IN('.$cats_list.') '."\n"; //print_r( count($globalcats[$top_cat]->descendantsarray) ); exit; } else { $join_cats = ''; $where_cats = ''; } $sql = 'S ELECT COUNT(DISTINCT i.id) AS items_total,'."\n" .' t.name AS type_name, t.id AS type_id'."\n" .' FROM # __content AS i'."\n" .' JOIN # __flexicontent_items_ext AS ext'."\n" .' ON i.id=ext.item_id'."\n" .' JOIN # __flexicontent_types AS t'."\n" .' ON t.id=ext.type_id'."\n" .$join_cats .' WHERE 1' .$where_cats .' GROUP BY t.id '; $db = JFactory::getDBO(); $db->setQuery($sql); $type_stats = $db->loadObjectList('type_id'); if ($db->getErrorNum()) { echo "Error in query:".$sql; echo $db->getErrorMsg(); } if ($type_stats) foreach( $type_stats as $tid => $tstats) { echo " Type id: ". $tid; echo " Type name: ". $tstats->type_name; echo " Total Items : ". $tstats->items_total; echo "<br/>"; }