Number subcategories in parent category ?

Phoca Gallery - image gallery extension
dr.archik
Phoca Member
Phoca Member
Posts: 23
Joined: 14 Aug 2009, 23:05

Number subcategories in parent category ?

Post 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
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 49299
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Number subcategories in parent category ?

Post by Jan »

Hi, I think, then you need some specific sql query to count of categories under the category (maybe some recursive). :idea:

Jan
If you find Phoca extensions useful, please support the project
imperialWicket
Phoca Enthusiast
Phoca Enthusiast
Posts: 78
Joined: 28 Feb 2010, 15:39

Re: Number subcategories in parent category ?

Post 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.
dr.archik
Phoca Member
Phoca Member
Posts: 23
Joined: 14 Aug 2009, 23:05

Re: Number subcategories in parent category ?

Post by dr.archik »

Excellently! Thank you very much! :twisted:

But it is a pity that it was necessary to use additional query :idea:
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 49299
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Number subcategories in parent category ?

Post by Jan »

Hi, thank you for this guide, marked as Improvement.

Jan
If you find Phoca extensions useful, please support the project
Post Reply