17
votes

Hello I have installed jbusinessdirectory component for joomla, and I have module named mod_jbusinessdirectory (this is a search module for business listing) in tmpl/default.php file I have select code: (see below)

<?php if($params->get('showCategories')){ ?>
    <div class="select">
        <div class="categoryic"></div>
        <select name="categorySearch" class="select-styled" id="categories">
    <option value="0">category</option>
    <?php foreach($categories as $category){?>
    <option value="<?php echo $category->id?>" <?php echo $session->get('categorySearch')==$category->id && $preserve?" selected ":"" ?> ><?php echo $category->name?></option>
    <?php if(!empty($category->subcategories)){?>
    <?php foreach($category->subcategories as $subCat){?>
    <option value="<?php echo $subCat->id?>" <?php  echo $session->get('categorySearch')==$subCat->id && $preserve?" selected ":"" ?> >-- <?php echo $subCat->name?></option>
    <?php }?>
      <?php }?>
      <?php }?>
    </select>
    </div>
 <?php }?>

From this code I get categories and subcategories like this:

  • Main category 1
  • subcategory 1 subcategory 2 subcategory 3

  • Main category 2

  • subcategory 1 subcategory 2 subcategory 3

screenshot here: categories and sub categories screenshot

In helper.php I have functions that get categories and subcategories from database

static function getMainCategories(){
    $db = JFactory::getDBO();
    $query = ' SELECT * FROM #__jbusinessdirectory_categories where parent_id=1 and published=1  order by name';
    $db->setQuery($query);
    return $db->loadObjectList();
}

static function getSubCategories(){
    $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.parent_id = 1 and c.published=1
               order by c.name';
    $db->setQuery($query,0,1000);
    $result = $db->loadObjectList();

    return $result;
}

And lastly in modjbusinesdirectory.php file I have the PHP like this:

if($params->get('showCategories')){
    $categories =  modJBusinessDirectoryHelper::getMainCategories();
    if($params->get('showSubCategories')){
        $subCategories = modJBusinessDirectoryHelper::getSubCategories();
        foreach($categories as $category){
            foreach($subCategories as $subCat){
                if($category->id == $subCat->parent_id){
                    if(!isset($category->subcategories)){
                        $category->subcategories = array();
                    }
                    $category->subcategories[] = $subCat;
                }
            }
        }
    }
}

categories and subcategories table structure screenshot here

My question is: How do I make Two select queries instead of one. Where in the first query I get the main categories and in the second query I get the subcategories (eg: if I choose from the first query the main category books and in the second query I choose children it has to show only books with the subcategory children books).

2
your English is fine sir only a couple of spelling errors but I understood what you meant and corrected those. Also have you tried to put the result of the first query into a variable and with the second query for example books. SELECT the columns you need FROM tablename WHERE MainCategoryColumn = $MainCategoryVariable AND SubcategoryColumn = $inputVariableBRoebie
Thank you for your answer, but I am beginner in php and I could not understand what you meanGocha Gochitashvili
Maincategory and subcategory have one column, but I have parent_id columnGocha Gochitashvili
Yes i.stack.imgur.com/7wVbQ.png here is table structure screenshotGocha Gochitashvili
yes I saw that in your question but I don't know which column has what purpose. but if I understand correctly you want to select everything out of that table. And parent_id stores the id of the main categoryBRoebie

2 Answers

1
votes

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;
            }
    }
0
votes

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.

<?php

static 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.

<?php
if($params->get('showCategories')){
    $categoryId = (int) $params->get('mainCategory');
    $category   =  modJBusinessDirectoryHelper::getCategoriesById($categoryId);

    $subCategories = modJBusinessDirectoryHelper::getChildCategoryList($categoryId);
    }
}