逻辑选择类别和子类别(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;
}
}
}
}
}
类别和子类别表结构屏幕截图在这里
我的问题是:如何进行两个选择查询而不是一个。在第一个查询中,我得到主要类别,在第二个查询中,我得到子类别(例如:如果我从第一个查询中选择主类别书籍,在第二个查询中选择子类书籍,它必须只显示子类别子类书籍)。