Logically select categories and subcategories (php,joomla,javascript,ajax)
You can use below query for get categories.
function all_cat($id='',$child_of_child='parent') { $CI =& get_instance(); if($id="") { $query = $CI->db->get_where("__jbusinessdirectory_categories",array('parent_id'=>1)); } else { $query = $CI->db->get_where("__jbusinessdirectory_categories",array('parent_id'=>$id)); } //echo $CI->db->last_query()."<br>"; $op = ''; if($query->num_rows() > 0) { $res = $query->result(); //pr($res); if($child_of_child == 'child') { $op .= '<ul class="sub_cat mrg-top-5" style="display:none;" id="'.$id.'">'; } else { $op .= '<ul class="sub_cat " id="'.$id.'">'; } foreach($res as $r) { $op .= '<li><a href="'.site_url('category/search/9/1V1/'.$r->id).'" class="temp">'.ucwords($r->name).'</a> </li>'; $op .= all_cat($r->id,$child_of_child='child'); } return $op .= '</ul>'; } else { return $op; } }
It appears that the code already does what you want. You want 2 queries and there are 2. But, I think the result of these 2 queries combines into 1 result and you want to keep it spit into 2 results.
You will have to add new category functions that accept category ID parameters.
<?phpstatic function getCategoryById($id){ $id = intval( $id ); $db = JFactory::getDBO(); $query = ' SELECT * FROM #__jbusinessdirectory_categories WHERE published=1 AND id= '.$id; $db->setQuery($query); return $db->loadObject();}static function getChildCategoryList($id){ $id = intval($id); $db = JFactory::getDBO(); $query = ' SELECT c.* FROM #__jbusinessdirectory_categories c INNER JOIN #__jbusinessdirectory_categories cc ON c.parent_id = cc.id WHERE c.parent_id!=1 AND cc.id = '.$id.' AND c.published=1 ORDER BY c.name'; $db->setQuery($query,0,1000); $result = $db->loadObjectList(); return $result;}
I am not familiar with Joomla code that retrieves parameters but you can find the ID if you POST it when someone selects a main category.
<?phpif($params->get('showCategories')){ $categoryId = (int) $params->get('mainCategory'); $category = modJBusinessDirectoryHelper::getCategoriesById($categoryId); $subCategories = modJBusinessDirectoryHelper::getChildCategoryList($categoryId); }}