Page 1 of 1

Number subcategories in parent category ?

Posted: 30 Jun 2010, 07:04
by dr.archik
I need to output Number subcategories in parent category, in the main page !
What code should be in file : www\templates\test\html\com_phocagallery\categories\default.php ?
Arrow: $this->categories[$i] - doesn't contain it (((

Please help ....
Image

Re: Number subcategories in parent category ?

Posted: 30 Jun 2010, 11:29
by Jan
Hi, I think, then you need some specific sql query to count of categories under the category (maybe some recursive). :idea:

Jan

Re: Number subcategories in parent category ?

Posted: 30 Jun 2010, 13:29
by imperialWicket
Jan is right, this should be a more complex query, and I will add that it should be added as a function in the categories model that only gets called based on a parameter (not as a procedure call in a loop in the categories template).

But, in the interest of functionality over form, here is a quick and dirty procedural code snippet that should work. Just add this in the appropriate layout area of /components/com_phocagallery/views/categories/tmpl/default.php (probably right before the "// Rating" comment):

Code: Select all

// SHOW SUBCATEGORIES COUNT -- BEGIN
    $db =& JFactory::getDBO();
     
    $query = "SELECT count(id) AS subcats FROM #__phocagallery_categories WHERE parent_id = ".$this->categories[$i]->id;
    $db->setQuery($query);
    $subcat_count = $db->loadResult();
    
    echo '<tr><td>'.JText::_('Number of subcategories') .': </td>'
		.'<td>'.$subcat_count.'</td></tr>';
// SHOW SUBCATEGORIES COUNT -- END
You could pretty easily alter this to list the titles of the sub-categories if you so desire.

Re: Number subcategories in parent category ?

Posted: 30 Jun 2010, 13:39
by dr.archik
Excellently! Thank you very much! :twisted:

But it is a pity that it was necessary to use additional query :idea:

Re: Number subcategories in parent category ?

Posted: 30 Jun 2010, 15:03
by Jan
Hi, thank you for this guide, marked as Improvement.

Jan