Skip to content

Instantly share code, notes, and snippets.

@khangvm53
Last active March 16, 2016 08:10
Show Gist options
  • Save khangvm53/53cf54d68a18a9169acb to your computer and use it in GitHub Desktop.
Save khangvm53/53cf54d68a18a9169acb to your computer and use it in GitHub Desktop.
joomla query get subcategory
<?php
// Get Params
$app = JFactory::getApplication();
$params = $app->getMenu()->getActive()->params; // get the active item
$category_ids = array($params->get('category_id'));
getSubCategories( array($params->get('category_id')), $category_ids, 4 );
var_dump($category_ids);
function getSubCategories($ids, &$catIDs, $level){
$languageTag = JFactory::getLanguage()->getTag();
$db = JFactory::getDbo();
foreach($ids as $id){
$cat_query = $db->getQuery(true);
$cat_query->select('id')
->from($db->quoteName('#__categories'))
->where('`language`=\'' . $languageTag . '\' OR `language`=\'*\'')
->where('`parent_id`=' . $id);
$categories = $db->setQuery($cat_query)
->loadAssocList();
$temp_id = array();
if(count($categories) > 0){
foreach ($categories as $cate) {
$temp_id[] = $cate['id'];
$catIDs[] = $cate['id'];
}
}
if($level >= 0 && count($temp_id) > 0){
$level--;
$this->getSubCategories($temp_id, $catIDs, $level);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment