逻辑选择类别和子类别(php,joomla,javascript,ajax)

2022-08-30 23:48:04

您好,我已经为joomla安装了jbusinessdirectory组件,并且在tmpl / default中安装了名为mod_jbusinessdirectory的模块(这是用于业务列表的搜索模块.php文件我有选择代码:(见下文)

<?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 }?>

从这段代码中,我得到了这样的类别和子类别:

  • 主要类别1
  • 子类别 1 子类别 2 子类别 3

  • 主要类别2

  • 子类别 1 子类别 2 子类别 3

屏幕截图在这里:类别和子类别屏幕截图

在帮助器中.php我有从数据库中获取类别和子类别的函数

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

最后,在modjbusinesdirectory.php文件中,我有这样的PHP:

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

类别和子类别表结构屏幕截图在这里

我的问题是:如何进行两个选择查询而不是一个。在第一个查询中,我得到主要类别,在第二个查询中,我得到子类别(例如:如果我从第一个查询中选择主类别书籍,在第二个查询中选择子类书籍,它必须只显示子类别子类书籍)。


答案 1

您可以使用以下查询来获取类别。

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

答案 2

代码似乎已经执行了所需的操作。您需要 2 个查询,并且有 2 个查询。但是,我认为这2个查询的结果合并为1个结果,并且您希望将其保留为2个结果。

您必须添加接受类别 ID 参数的新类别函数。

<?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;
}

我不熟悉检索参数的Joomla代码,但是当您在有人选择主要类别时发布它,则可以找到ID。

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

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